<?php

namespace app\controller;

use app\BaseController;
use app\message;
use app\model\ClueCommunicateRecord;
use app\model\ClueMobile;
use app\model\CluePerson;
use app\model\CluePersonAllocationRecord;
use app\model\CluePersonCertificate;
use app\model\OrderDeal;
use app\model\OrderDealChargeback;
use app\model\OrderEnterpriseCertificateBusiness;
use app\model\OrderEnterpriseCertificateRegister;
use app\model\OrderEnterpriseCompany;
use app\model\OrderEnterpriseExchange;
use app\model\OrderLog;
use app\model\OrderOtherCertificateBusiness;
use app\model\OrderPersonCertificateRegister;
use app\model\OrderTransactionLog;
use app\model\OrderTransactionRecord;
use app\model\SystemCertificate;
use app\model\SystemOperateLog;
use app\model\User;
use app\model\UserOrganization;
use app\Request;
use app\service\Certificate;
use app\service\Clue;
use app\service\FinanceServer;
use app\service\Power;
use app\service\RedisService;
use app\service\System;
use app\stateData;
use app\validate\FinanceValidate;
use GatewayClient\Gateway;
use think\Exception;
use think\exception\ValidateException;
use think\facade\Cache;
use think\facade\Db;
use think\facade\Log;
use think\facade\Validate;
use function Symfony\Component\VarDumper\Dumper\esc;

class Finance extends BaseController
{

    private $Collection = 1;//收款
    private $CollectionCode = 'SKJL';

    private $Payment = 2;//付款
    private $PaymentCode = 'FKJL';//付款

    private $pressMoneyTitle = '催款提醒';
    private $messages = '您有一条催款提醒，请及时处理;';
    private $message_type = 7; //催款类别


    /**
     * 财务
     */
    //添加付款
    public function addPayMent(
        Request                $request,
        OrderTransactionRecord $transaction,
        Certificate            $certificate,
        OrderDeal              $deal,
        User                   $user,
        \app\model\UserMessage $message,
        Gateway                $gateway,
        OrderLog               $log,
        OrderTransactionLog    $orderTransactionLogModel,
        Image                  $image,
        SystemOperateLog $systemOperateLogModel
    ){

        $param = $request->param();
        $uid = $request->param('user_login_id');

        //财务订单编号
        $financeCode = $certificate->CertificateCodeCreate($this->PaymentCode);
        $annex = $param['annex'];
        $param['annex'] = '';
        $param['annex_name'] = '';
        //上传资料
        if (!empty($annex)) {
            $annex = json_decode($annex, true);
            $annex = $image->handleImgs($annex);
            $param['annex'] = $annex['path'];
            $param['annex_name'] = $annex['file_name'];
        }

        Db::startTrans();
        try {

            $result = [];
            $data = json_decode($param['content'], true);
            $bool = true;
            foreach ($data as $key => $value) {

                if (empty($value['id'])) {
                    return ReJson(message::$ERROR_CODE, '订单id不能为空');
                }
                if (empty($value['price'])) {
                    return ReJson(message::$ERROR_CODE, '金额不能为0');
                }

                $result[$key]['certificate_code'] = $value['certificate_code'];
                $result[$key]['order_company_id'] = $value['id'];
                $result[$key]['price'] = $value['price'];

                //判断该订单id是否已经成交
//                $dealData = $deal->getOrderCerFindModel($result[$key]['certificate_code']);

//                if (empty($dealData)){
//                    return ReJson (message::$ERROR_CODE,message::$ORDER_NOT_DEAL);
//                }

                //判断成交id是否为0 如果不为0 说明是企业证书注册业务 再查询companyId
//                if ($dealData['order_company_id'] !=0 ){
                //是否存在该业务id订单
//                    if (empty($deal->getOrderCompanyIdFindModel($dealData['order_company_id'],$value))){
//                        return ReJson (message::$ERROR_CODE,message::$ORDER_NOT_DEAL);
//                    }
//                }

                //拼接参数
                $result[$key]['transaction_code'] = $financeCode;
                $result[$key]['transaction_type'] = $this->Payment;
                $result[$key]['transaction_org'] = $user->getUidOrgId($uid);
                $result[$key]['transaction_uid'] = $uid;
                $result[$key]['examine_uid'] = $param['examine_uid'];
                $result[$key]['transaction_date'] = $param['transaction_date'];
//                $result[$key]['submit_date'] = $param['submit_date'];
                $result[$key]['type'] = $param['type'];
                $result[$key]['settlement_method'] = $param['settlement_method'];
                $result[$key]['annex'] = $param['annex'];
                $result[$key]['annex_name'] = $param['annex_name'];

                if (!empty($value['notes'])) {
                    $result[$key]['notes'] = $value['notes'];
                }
                //验证参数
                Validate(\app\validate\Finance::class)->check($result[$key]);

                //添加付款申请
                $data = $transaction->addDataModel($result[$key]);

                //记录到订单 -收付款日志
                $log->addDataModel([
                    'order_company_id' => $value['id'],
                    'message' => message::$ORDER_PAYMENT_ADD,
                    'certificate_code' => $value['certificate_code'],
                    'submit_id' => $uid
                ]);
                if ($bool) {
                    //添加收付款日志
                    $orderTransactionLogModel->createModel([
                        'tid' => $data->id,
                        'content' => '添加付款',
                        'submit_id' => $uid
                    ]);
                    //添加消息列表
                    $messages = $message->addPayMentModel($data['transaction_code'], $data['examine_uid']['id'], $uid, $data['id']);
                    //发送消息
                    $gateway->sendToUid($data['examine_uid']['id'], json_encode($messages));
                    $bool = false;
                }
            }

            $systemOperateLogModel->addOperate ($uid,message::$KHGL,'添加付款-编号'.$data['transaction_code']);
            Db::commit();
        } catch (\Exception|ValidateException $e) {
            Db::rollback();
            return ReJson(message::$ERROR_CODE, $e->getMessage());
        }
        return ReJson(message::$SUCC_CODE, message::$SUCC_INSERT_DATA);
    }
    //修改收付款
    public function editCollection(
        Request                $request,
        OrderTransactionRecord $transaction,
        Image                  $image,
        Gateway                $gateway,
        \app\model\UserMessage $messageModel,
        OrderTransactionLog    $orderTransactionLogModel,
        SystemOperateLog $systemOperateLogModel,
        RedisService $redisService
    ){
        $param = $request->param();
        $id = $request->post('id');
        $uid = $request->post('user_login_id');
        //上传资料
        if (isset($param['annex'])) {
            $array = $image->handle($param['annex'], 'annex', 'annex_name');
            $param['annex'] = $array['annex'];
            $param['annex_name'] = $array['annex_name'];
        }

        $data = json_decode($param['content'], true);
        $codeStr = '';
        foreach ($data as $key => $value) {
            $result[$key]['certificate_code'] = $value['certificate_code'];
//            $result[$key]['order_company_id'] = $value['id'];
            $result[$key]['price'] = $value['price'];
            //拼接参数
            $result[$key]['examine_uid'] = $param['examine_uid'];
            $result[$key]['transaction_date'] = $param['transaction_date'];
            $result[$key]['type'] = $param['type'];
            if (isset($param['submit_date'])) {
                $result[$key]['submit_date'] = $param['submit_date'];
            }
            $result[$key]['settlement_method'] = $param['settlement_method'];
            $result[$key]['annex'] = $param['annex'];
            $result[$key]['annex_name'] = $param['annex_name'];
            $result[$key]['notes'] = $value['notes'];

            //编辑status 为待审核
            $result[$key]['status'] = 0;


            //验证参数
//            Validate(\app\validate\Finance::class)->check ($result[$key]);
            $editData = $transaction->editDataModel($id, $result[$key]);
            if ($param['transaction_type'] == 1) { //收款
                //添加记录日志
                $orderTransactionLogModel->createModel([
                    'tid' => $id,
                    'content' => '编辑收款',
                    'submit_id' => $uid
                ]);
                //添加消息列表
                $messages = $messageModel->addCollectionModel($param['transaction_code'], $param['examine_uid'], $uid, $param['id']);
                //发送消息
                $gateway->sendToUid($param['examine_uid'], json_encode($messages));
            } else { //付款
                //添加记录日志
                $orderTransactionLogModel->createModel([
                    'tid' => $id,
                    'content' => '编辑付款',
                    'submit_id' => $uid
                ]);
                //添加消息列表
                $messages = $messageModel->addCollectionPassModel($param['transaction_code'], $param['examine_uid'], $uid, $param['id']);
                //发送消息
                $gateway->sendToUid($param['examine_uid'], json_encode($messages));
            }
            /**
             * 删除缓存
             */
            $redisService->delTransactionId ($id);

        }
        $systemOperateLogModel->addOperate ($uid,message::$ORDER,'收付款编辑');
        return ReJson(message::$SUCC_CODE, '收付款编辑成功');
    }

    public function or_url($url)
    {

        $preg = "/http[s]?:\/\/[\w.]+[\w\/]*[\w.]*\??[\w=&\+\%]*/is";
        $flag = false;
        if (preg_match($preg, $url)) {
            $flag = true;
        }
        return $flag;

    }

    //添加收款
    public function addCollection(
        Request                $request,
        OrderTransactionRecord $transaction,
        Certificate            $certificate,
        OrderDeal              $deal,
        User                   $user,
        \app\model\UserMessage $message,
        Gateway                $gateway,
        OrderLog               $log,
        OrderTransactionLog    $orderTransactionLogModel,
        Image                  $image,
        SystemOperateLog $systemOperateLogModel
    ){
        $param = $request->param();
        $uid = $request->param('user_login_id');

        $financeCode = $certificate->CertificateCodeCreate($this->CollectionCode);

        $annex = $param['annex'];
        $param['annex'] = '';
        $param['annex_name'] = '';
        //上传资料
        if (!empty($annex)) {
            $annex = json_decode($annex, true);
            $annex = $image->handleImgs($annex);
            $param['annex'] = $annex['path'];
            $param['annex_name'] = $annex['file_name'];
        }
        Db::startTrans();
        try {
            $bool = true;
            $result = [];
            $data = json_decode($param['content'], true);
            //判断参数是否有误
            foreach ($data as $key => $value) {
                if (empty($value) || empty($value['id']) || (empty($value['price']) && (int)$value['price'] != 0)) return ReJson(message::$ERROR_CODE, message::$ERR_PARAM_EMPTY);

                $result[$key]['certificate_code'] = $value['certificate_code'];
                $result[$key]['order_company_id'] = $value['id'];
                $result[$key]['price'] = $value['price'];

                //判断该订单id是否已经成交
//                $dealData = $deal->getOrderCerFindModel($result[$key]['certificate_code']);

//                if (empty($dealData)){
//                    return ReJson (message::$ERROR_CODE,message::$ORDER_NOT_DEAL);
//                }

                //判断成交id是否为0 如果不为0 说明是企业证书注册业务 再查询companyId
//                if ($dealData['order_company_id'] !=0 ){
                //是否存在该业务id订单
//                    if (empty($deal->getOrderCompanyIdFindModel($dealData['order_company_id'],$value))){
//                        return ReJson (message::$ERROR_CODE,message::$ORDER_NOT_DEAL);
//                    }
//                }

                //拼接参数
                $result[$key]['transaction_code'] = $financeCode;
                $result[$key]['transaction_type'] = $this->Collection;
                $result[$key]['transaction_org'] = $user->getUidOrgId($uid);
                $result[$key]['transaction_uid'] = $uid;
                $result[$key]['examine_uid'] = $param['examine_uid'];
                $result[$key]['submit_date'] = $param['submit_date'];
                $result[$key]['transaction_date'] = $param['transaction_date'];
                $result[$key]['type'] = $param['type'];
                $result[$key]['settlement_method'] = $param['settlement_method'];
                $result[$key]['annex'] = $param['annex'];
                $result[$key]['annex_name'] = $param['annex_name'];


                if (!empty($value['notes'])) {
                    $result[$key]['notes'] = $value['notes'];
                }

                //验证参数
                Validate(\app\validate\Finance::class)->check($result[$key]);

                $data = $transaction->addDataModel($result[$key]);

                //记录到订单 -收付款日志
                $log->addDataModel([
                    'order_company_id' => $value['id'],
                    'message' => message::$ORDER_COLLECTION_ADD,
                    'certificate_code' => $value['certificate_code'],
                    'submit_id' => $uid
                ]);

                if ($bool) {
                    //添加收付款日志
                    $orderTransactionLogModel->createModel([
                        'tid' => $data->id,
                        'content' => '添加收款',
                        'submit_id' => $uid
                    ]);

                    //添加消息列表
                    $messages = $message->addCollectionModel($data['transaction_code'], $data['examine_uid']['id'], $uid, $data['id']);
                    //发送消息
                    $gateway->sendToUid($data['examine_uid']['id'], json_encode($messages));
                    $bool = false;
                }
            }

            $systemOperateLogModel->addOperate ($uid,message::$ORDER,'添加收款'.$data['transaction_code']);
            Db::commit();
        } catch (\Exception|ValidateException $e) {
            Db::rollback();
            return ReJson(message::$ERROR_CODE, $e->getMessage());
        }
        return ReJson(message::$SUCC_CODE, message::$SUCC_INSERT_DATA);
    }

    //删除收付款
    public function delTransaction(
        Request                $request,
        OrderTransactionRecord $transactionRecord,
        SystemOperateLog $systemOperateLogModel,
        RedisService $redisService,
        OrderEnterpriseCompany $enterpriseCompanyModel,
        OrderEnterpriseExchange $orderEnterpriseExchangeModel,
        OrderEnterpriseCertificateBusiness $orderEnterpriseCertificateBusinessModel,
        OrderOtherCertificateBusiness $otherCertificateBusinessModel
    ){
        $id = $request->post('id');
        $uid = $request->post ('user_login_id');
        if (empty($id)) return ReJson(message::$ERROR_CODE, message::$ERR_PARAM_EMPTY);

        $idArr = explode(',', $id);
        foreach ($idArr as $item) {
            //查询收付款编号相关的收付款 全部删除
            $find = $transactionRecord->getIdDataModel($item);
            $tran_idArr = $transactionRecord->getIdDataSModel($find['transaction_code']);

//            foreach ($tran_idArr as $value) {
            $systemOperateLogModel->addOperate ($uid,message::$ORDER,'删除收付款'.$id);
            $transactionRecord->delTransactionModel($tran_idArr);
            /**
             * 删除缓存
             */
            $redisService->delFlowingId ($find['id']);
            $redisService->delTransactionId ($find['id']);
            /**
             * 删除财务管理-业绩总表缓存
             */
            //查询订单属于哪个订单 删除对应销售时间证书编号缓存
            $saleData = $enterpriseCompanyModel->getCertificateCodeFindModel ($find['certificate_code']);
            if ($saleData){ //公司证书
                $redisService->delPerformance($saleData);
            }

            $saleData = $orderEnterpriseExchangeModel->getWhereCerDataModel ($find['certificate_code']);
            if ($saleData){ //倒证
                $redisService->delPerformance($saleData);
            }

            $saleData = $orderEnterpriseCertificateBusinessModel->getCerDataModel ($find['certificate_code']);
            if ($saleData){ //企业证书
                $redisService->delPerformance($saleData);
            }

            $saleData = $otherCertificateBusinessModel->getCerDataModel ($find['certificate_code']);
            if ($saleData){ //证书办理
                $redisService->delPerformance($saleData);
            }

//            }
        }
        return ReJson(message::$SUCC_CODE, message::$SUCC_DATELE_DATA);

    }

    //编辑收付款
    public function editTransaction(
        Request                $request,
        OrderTransactionRecord $transactionRecord,
        Image                  $image,
        RedisService $redisService
    ){
        $param = $request->post();
        if (empty($param['id'])) return ReJson(message::$ERROR_CODE, message::$ERR_PARAM_EMPTY);
        //文件上传处理
        $array = $image->handle($param['annex'], 'annex', 'annex_name');
        $param['annex'] = $array['annex'];//文件地址
        $param['annex_name'] = $array['annex_name'];//文件名
        $transactionRecord->editTransactionModel($param);

        $redisService->delTransactionId ($param['id']);

        return ReJson(message::$SUCC_CODE, message::$SUCC_UPDATE);
    }

    //设置财务审批人
    public function setExamineData()
    {

    }

    //获取审批人列表  默认财务
    public function getExamineList(
        FinanceServer $financeServer,
        Request       $request
    )
    {
        ini_set ('memory_limit',-1);
        $uid = $request->param('user_login_id');
        $result = $financeServer->getSpPersonList($uid);
        return ReJson(message::$SUCC_CODE, message::$SUCC_SELECT_DATA, $result);
    }

    /***********************************************************************
     * 订单操作
     */
    //退单操作
    public function chargeBack(
        Request                            $request,
        OrderEnterpriseCertificateBusiness $business,
        OrderOtherCertificateBusiness      $otherCerBusiness,
        OrderEnterpriseCompany             $company,
        OrderEnterpriseExchange            $exchange,
        OrderEnterpriseCertificateRegister $orderEnterpriseCertificateRegisterModel,
        OrderDealChargeback                $chargeback,
        \app\model\UserMessage             $message,
        Gateway                            $gateway,
        OrderLog                           $orderLog,
        OrderPersonCertificateRegister     $OrderPersonCertificateRegister,
        CluePerson                         $personModel,
        Image                              $image,
        RedisService $redisService,
        SystemOperateLog $systemOperateLogModel
    ){
        $certificateCode = $request->post('certificate_code');
        $uid = $request->post('user_login_id');
        $param = $request->post();

//        $file = $request->file('get_uid_up_logoff_file');

        Db::startTrans();
        try {
            if (empty($certificateCode) || empty($param['notifier_get_uid'])) return ReJson(message::$ERROR_CODE, message::$ERR_PARAM_EMPTY);

//            if ($file){
//                // 上传到本地服务器
//                $param['get_uid_up_logoff_file'] = \think\facade\Filesystem::disk('public')->putFile( 'topic', $file);
//            }

            $array = $image->handle($param['get_uid_up_logoff_file'], 'get_uid_up_logoff_file', 'get_uid_up_logoff_file_name');
            $param['get_uid_up_logoff_file'] = $array['get_uid_up_logoff_file'];
            $param['get_uid_up_logoff_file_name'] = $array['get_uid_up_logoff_file_name'];

            $btype = 0;
            $oid = 0;
            //查询是否是企业证书成交
            if (!empty($business->getCerFieldDataModel($certificateCode))) {
                $btype = 5;
                //查看订单是否为已完成订单
                $orderData = $business->getCerDataModel($certificateCode);

//                if ($orderData['status']['id'] != 1){
//                    return ReJson (message::$ERROR_CODE,message::$ORDER_NOHAVE_STATUS_DEAL);
//                }
                if (!$orderData['performance']) {
                    $orderData['performance'] = 0;
                }
                //添加原订单业绩
                $param['performance'] = $orderData['performance'] * -1;

                //修改订单状态
//                $business->setStatusBack($certificateCode);
                $chargeback->addDataModel($param);

            }

            //查询是否是证书办理成交
            if (!empty($otherCerBusiness->getFinanceFieldFindModel($certificateCode))) {
                $btype = 4;
                //查看订单是否为已完成订单
                $orderData = $otherCerBusiness->getCerDataModel($certificateCode);
//                if ($orderData['status']['id'] != 1){
//                    return ReJson (message::$ERROR_CODE,message::$ORDER_NOHAVE_STATUS_DEAL);
//                }
                if (!$orderData['performance']) {
                    $orderData['performance'] = 0;
                }
                //添加原订单业绩
                $param['performance'] = $orderData['performance'] * -1;
                //修改订单状态
//                $otherCerBusiness->setStatusBack($certificateCode);
                $chargeback->addDataModel($param);

            }

            //查询是否是企业证书注册
            if (!empty($company->getCertificateCodeFindModel($certificateCode))) {
                $btype = 2;
                //查看订单是否为已完成订单
                $orderData = $company->getFinanceFieldIdFindModel($param['order_company_id'], $certificateCode);
                $oid = $orderData['id'];
//                if ($orderData['status']['id'] != 1){
//                    return ReJson (message::$ERROR_CODE,message::$ORDER_NOHAVE_STATUS_DEAL);
//                }
                if (!$orderData['performance']) {
                    $orderData['performance'] = 0;
                }
                //添加原订单业绩
                $param['performance'] = $orderData['performance'] * -1;

                //修改订单状态
//                $company->setStatusBack($certificateCode);
                $chargeback->addDataModel($param);

//                //需求人数加一
//                $orderEnterpriseCertificateRegisterModel->peopleNumIncModel($param['order_company_id']);
//                //修改人才信息为待审核person_id_card
//                $OrderPersonCertificateRegister->saveStatusIdCardModel($orderData['person_id_card']);
//                //修改线索库人才为待出售
//                $personModel->setIdCardDcsStatus($orderData['person_id_card']);
            }

            //查询是否是倒证成交
            if (!empty($exchange->getFinanceFieldFindModel($certificateCode))) {
                $btype = 3;

                //查看订单是否为已完成订单
                $orderData = $exchange->getFinanceFieldFindModel($certificateCode);
//                if ($orderData['status']['id'] != 1){
//                    return ReJson (message::$ERROR_CODE,message::$ORDER_NOHAVE_STATUS_DEAL);
//                }
                if (!$orderData['performance']) {
                    $orderData['performance'] = 0;
                }
                //添加原订单业绩
                $param['performance'] = $orderData['performance'] * -1;

                //修改订单状态
//                $exchange->setStatusBack($certificateCode);
                $chargeback->addDataModel($param);
            }

            //添加订单日志
            $orderLog->addDataModel([
                'submit_id' => $uid,
                'message' => message::$ORDER_FAQI_TUIDAN,
                'type' => 1,
                'order_company_id' => $param['order_company_id'],
                'certificate_code' => $param['certificate_code']
            ]);

            //添加信息列表
            $messages = $message->addChargebackModel($param['notifier_get_uid'], $certificateCode, $uid, $btype, $oid, '');

            //发送消息
            $gateway->sendToUid($param['notifier_get_uid'], json_encode($messages));
            $systemOperateLogModel->addOperate ($uid,message::$ORDER,'发起退单'.$certificateCode);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return ReJson(message::$ERROR_CODE, $e->getMessage());
        }

        return ReJson(message::$SUCC_CODE, message::$SUCC_BACK);
    }


    //退单第二步
    public function chargeBackSupp(
        Request                            $request,
        OrderEnterpriseCertificateBusiness $business,
        OrderOtherCertificateBusiness      $otherCerBusiness,
        OrderEnterpriseCompany             $company,
        OrderEnterpriseExchange            $exchange,
        OrderEnterpriseCertificateRegister $orderEnterpriseCertificateRegisterModel,
        OrderDealChargeback                $chargeback,
        \app\model\UserMessage             $message,
        Gateway                            $gateway,
        OrderLog                           $orderLog,
        OrderPersonCertificateRegister     $OrderPersonCertificateRegister,
        CluePerson                         $personModel,
        Image                              $image,
        SystemCertificate                  $systemCertificateModel,
        RedisService $redisService,
        SystemOperateLog $systemOperateLogModel
    ){
        $param = $request->post();
        $uid = $request->post('user_login_id');

        //第二步补充退单 is_complete 为1 已完成
        $param['is_complete'] = 1;

        if (empty($param['certificate_code']) || empty($param['order_company_id'])) return ReJson(message::$ERROR_CODE, message::$ERR_PARAM_EMPTY);

        $certificateCode = $param['certificate_code'];

//        $file = $request->file('push_uidup_logoff_file');
//
//        if ($file){
//            // 上传到本地服务器
//            $param['push_uidup_logoff_file'] = \think\facade\Filesystem::disk('public')->putFile( 'topic', $file);
//        }

        $array = $image->handle($param['push_uidup_logoff_file'], 'push_uidup_logoff_file', 'push_uidup_logoff_file_name');
        $param['push_uidup_logoff_file'] = $array['push_uidup_logoff_file'];
        $param['push_uidup_logoff_file_name'] = $array['push_uidup_logoff_file_name'];

        $order_company_id = '';
        $btype = 0;
        $oid = 0;
        $orderUid = 0;
        $startDate = 0;
        $endDate = 0;

        Db::startTrans();
        try {
            //查询是否是企业证书成交
            if (!empty($business->getCerFieldDataModel($certificateCode))) {
                $btype = 5;
                //查看订单是否为已完成订单
                $orderData = $business->getCerDataModel($certificateCode);
                //绑定订单业绩归属人|业绩计算时间
                $orderUid = $orderData['performance_uid'];
                $startDate = $endDate =  $orderData['sale_date'];
                $order_company_id = $orderData['id'];

//                if ($orderData['status']['id'] != 1){
//                    return ReJson (message::$ERROR_CODE,message::$ORDER_NOHAVE_STATUS_DEAL);
//                }
                //添加原订单业绩
//            $param['performance'] = $orderData['performance'];

                //修改订单状态
                $business->setStatusBack($certificateCode);
                //修改退单记录内容
                $chargeback->editCerDataModel($certificateCode, $param);
                //删除倒证订单缓存
                $redisService->delOrderDzData ($orderData,'order_qyzs');
                //删除业绩总表缓存
                $redisService->delPerformance ($orderData);
            }

            //查询是否是证书办理成交
            if (!empty($otherCerBusiness->getFinanceFieldFindModel($certificateCode))) {
                $btype = 4;
                //查看订单是否为已完成订单
                $orderData = $otherCerBusiness->getCerDataModel($certificateCode);
                //绑定订单业绩归属人|业绩计算时间
                $orderUid = $orderData['performance_uid'];
                $startDate = $endDate =  $orderData['sale_date'];
                $order_company_id = $orderData['id'];
//                if ($orderData['status']['id'] != 1){
//                    return ReJson (message::$ERROR_CODE,message::$ORDER_NOHAVE_STATUS_DEAL);
//                }
                //添加原订单业绩
//            $param['performance'] = $orderData['performance'];
                //修改订单状态
                $otherCerBusiness->setStatusBack($certificateCode);
                //修改退单记录内容
                $chargeback->editCerDataModel($certificateCode, $param);
                //修改人才为已注销
//                $personModel->setIdCardStatus($orderData['person_id_card'],4);
                //删除倒证订单缓存
                $redisService->delOrderDzData ($orderData,'order_zsbl');
                //删除业绩总表缓存
                $redisService->delPerformance ($orderData);

            }

            //查询是否是企业证书注册
            if (!empty($company->getCertificateCodeFindModel($certificateCode))) {

                $btype = 2;
                //查看订单是否为已完成订单
                $orderData = $company->getFinanceFieldIdFindModel($param['order_company_id'], $certificateCode);
                //绑定订单业绩归属人|业绩计算时间
                $orderUid = $orderData['performance_uid'];
                $startDate = $endDate =  $orderData['sale_date'];
                $oid = $orderData['id'];
                $order_company_id = $orderData['id'];

//                if ($orderData['status']['id'] != 1){
//                    return ReJson (message::$ERROR_CODE,message::$ORDER_NOHAVE_STATUS_DEAL);
//                }
                //添加原订单业绩
//            $param['performance'] = $orderData['performance'];
                //修改订单状态
                $company->setStatusBack($certificateCode);
                //修改退单记录内容
                $chargeback->editCerDataModel($certificateCode, $param);

                //需求人数加一
//                $orderEnterpriseCertificateRegisterModel->peopleNumIncModel($orderData['enter_id']);
                //修改人才信息为待审核person_id_card
//            $OrderPersonCertificateRegister->saveStatusIdCardModel($orderData['person_id_card']);
                //获取证书专业ID
                if (isset($orderData['certificate_id'][3])) {
                    $cerWhere = ['certificate_zhuanye' => $orderData['certificate_id'][3]];
                } else if (isset($orderData['certificate_id'][2])) {
                    $cerWhere = ['certificate_dengji' => $orderData['certificate_id'][2]];
                } else if (isset($orderData['certificate_id'][1])) {
                    $cerWhere = ['certificate_leibie' => $orderData['certificate_id'][1]];
                } else {
                    $cerWhere = ['certificate_leixing' => $orderData['certificate_id'][0]];
                }

                //修改人才信息为已退单person_id_card
                $OrderPersonCertificateRegister->saveStatusIdCardCharModel($cerWhere, $orderData['person_id_card']);
                //修改线索库人才为待出售
                $personModel->setIdCardStatus($orderData['person_id_card'], 4);

                //修改缓存
                $redisService->updateOrderZsshData ($OrderPersonCertificateRegister->getPersonIdCardOrderModel ($orderData['person_id_card'])['id']);
                //删除人才缓存
                $redisService->delPersonData ($personModel->getPersonIdCardInfo ($orderData['person_id_card'])['id']);
                //删除公司证书订单缓存
                $redisService->delOrderDzData ($orderData,'order_gszs');
                //删除业绩总表缓存
                $redisService->delPerformance ($orderData);

            }

            //查询是否是倒证成交
            if (!empty($exchange->getFinanceFieldFindModel($certificateCode))) {
                $btype = 3;
                //查看订单是否为已完成订单
                $orderData = $exchange->getFinanceFieldFindModel($certificateCode);
                //绑定订单业绩归属人|业绩计算时间
                $orderUid = $orderData['performance_uid'];
                $startDate = $endDate =  $orderData['sale_date'];
                $order_company_id = $orderData['id'];

//                if ($orderData['status']['id'] != 1){
//                    return ReJson (message::$ERROR_CODE,message::$ORDER_NOHAVE_STATUS_DEAL);
//                }
                //添加原订单业绩
//            $param['performance'] = $orderData['performance'];

                //修改订单状态
                $exchange->setStatusBack($certificateCode);
                //修改退单记录内容
                $chargeback->editCerDataModel($certificateCode, $param);
                //修改线索库人才为待出售
                $personModel->setIdCardStatus($orderData['id_card'], 4);
                //删除倒证订单缓存
                $redisService->delOrderDzData ($orderData,'order_dz');
                //删除业绩总表缓存
                $redisService->delPerformance ($orderData);
            }

            //添加订单日志
            $orderLog->addDataModel([
                'submit_id' => $uid,
                'message' => message::$ORDER_TUIDAN,
                'type' => 1,
                'order_company_id' => $order_company_id,
                'certificate_code' => $param['certificate_code']
            ]);

            /**
             * 更新业绩统计缓存
             */
            if ($orderUid && $startDate && $endDate){
                $redisService->updateDateBetweenData ($startDate,$endDate);
            }
            //添加信息列表
            $messages = $message->addChargebackModel($param['notifier_push_uid'], $param['certificate_code'], $uid, $btype, $oid, '');
            //发送消息
            $gateway->sendToUid($param['notifier_push_uid'], json_encode($messages));
            $systemOperateLogModel->addOperate ($uid,message::$ORDER,'退单完成-补充信息'.$certificateCode);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return ReJson(message::$ERROR_CODE, $e->getMessage());
        }

        return ReJson(message::$SUCC_CODE, message::$SUCC_OPERATE);
    }

    //注销第一步
    public function cerRegLogOff(
        Request                        $request,
        OrderEnterpriseCompany         $company,
        OrderEnterpriseExchange        $exchangeModel,
        OrderLog                       $orderLog,
        \app\model\UserMessage         $message,
        Gateway                        $gateway,
        OrderPersonCertificateRegister $certificateRegister,
        OrderDealChargeback            $chargeback,
        Image                          $image,
        SystemOperateLog $systemOperateLogModel
    ){
        $param = $request->post();
        $uid = $request->post('user_login_id');

        if (empty($param['certificate_code']) || empty($param['notifier_get_uid']) || empty($param['order_company_id'])) return ReJson(message::$ERROR_CODE, message::$ERR_PARAM_EMPTY);

        //发起注销操作
        Db::startTrans();
        try {
            $oid = 0;
            //查询此证书是倒证还是证书注册
            $cerData = $company->getIdCertificateCodeFindModel($param['order_company_id'], $param['certificate_code']);

            if (empty($cerData)) {
                $cerData = $exchangeModel->getWhereIdOrderByCreateData($param['order_company_id']);
                if ($cerData['status'] < 3 && $cerData['status'] != 0) return ReJson(message::$ERROR_CODE, message::$ORDER_CERTIFICATE_NO);

                $file = $request->file('get_uid_up_logoff_file');

                if ($file) {
                    // 上传到本地服务器
                    $param['get_uid_up_logoff_file'] = \think\facade\Filesystem::disk('public')->putFile('topic', $file);
                }

//                //注销
//                $exchangeModel->setLogOffModel($param['order_company_id'],$param['certificate_code']);
//                //添加记录
//                $orderLog->addDataModel([
//                    'submit_id'=>$request->param ('user_login_id'),
//                    'message'=>message::$ORDER_CERTIFICATE_LOG_OFF,
//                    'content'=>'',
//                    'type'=>2,
//                    'order_company_id'=>$param['order_company_id'],
//                    'certificate_code'=>$param['certificate_code']
//                ]);
//
//                //通过证书编号查询人才身份证
//                $idCard = $exchangeModel->getCerIdcardFindModel ($param['order_company_id'],$param['certificate_code']);
//                //注销以后该证书修改为优选证书 //注销以后该证书修改为待审核
//                $certificateRegister->becomeLogOff ($idCard);
                $btype = 2;
            } else {
                if ($cerData['status'] < 3 && $cerData['status'] != 0) return ReJson(message::$ERROR_CODE, message::$ORDER_CERTIFICATE_NO);
//            if ($file){
//                // 上传到本地服务器
//                $param['get_uid_up_logoff_file'] = \think\facade\Filesystem::disk('public')->putFile( 'topic', $file);
//            }

                $array = $image->handle($param['get_uid_up_logoff_file'], 'get_uid_up_logoff_file', 'get_uid_up_logoff_file_name');
                $param['get_uid_up_logoff_file'] = $array['get_uid_up_logoff_file'];
                $param['get_uid_up_logoff_file_name'] = $array['get_uid_up_logoff_file'];

                $file = $request->file('get_uid_up_logoff_file');

                if ($file) {
                    // 上传到本地服务器
                    $param['get_uid_up_logoff_file'] = \think\facade\Filesystem::disk('public')->putFile('topic', $file);
                }

//                //注销
//                $company->setLogOffModel($param['order_company_id'],$param['certificate_code']);
//                //添加记录
//                $orderLog->addDataModel([
//                    'submit_id'=>$request->param ('user_login_id'),
//                    'message'=>message::$ORDER_CERTIFICATE_LOG_OFF,
//                    'content'=>'',
//                    'type'=>2,
//                    'order_company_id'=>$param['order_company_id'],
//                    'certificate_code'=>$param['certificate_code']
//                ]);
//
//                //通过证书编号查询人才身份证
//                $idCard = $company->getCerIdcardFindModel ($param['order_company_id'],$param['certificate_code']);
//                //注销以后该证书修改为优选证书 //注销以后该证书修改为待审核
//                $certificateRegister->becomeLogOff ($idCard);
                $btype = 3;
            }
            $oid = $cerData['id'];
            //添加注销表
            $chargeback->addDataModel($param);
            //添加信息列表
            $messages = $message->addLogOffModel($param['notifier_get_uid'], $param['certificate_code'], $uid, $btype, $oid, '');
            //发送消息
            $gateway->sendToUid($param['notifier_get_uid'], json_encode($messages));
            $systemOperateLogModel->addOperate ($uid,message::$ORDER,'发起注销'.$param['certificate_code']);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }
        return ReJson(message::$SUCC_CODE, message::$SUCC_OPERATE);
    }

    //注销第二步补全资料
    public function cerRegLogOffSupp(
        Request                        $request,
        OrderEnterpriseCompany         $company,
        OrderDealChargeback            $chargeback,
        \app\model\UserMessage         $message,
        Gateway                        $gateway,
        OrderEnterpriseExchange        $exchangeModel,
        OrderLog                       $orderLog,
        OrderPersonCertificateRegister $certificateRegister,
        Image                          $image,
        OrderPersonCertificateRegister $certificateRegisterModel,
        CluePerson                     $personModel,
        RedisService $redisService,
        SystemOperateLog $systemOperateLogModel
    ){
        $param = $request->post();

        $uid = $request->post('user_login_id');

        if (empty($param['certificate_code'])) return ReJson(message::$ERROR_CODE, message::$ERR_PARAM_EMPTY);

        // $file = $request->file('push_uidup_logoff_file');

        if ($param['push_uidup_logoff_file']) {
            // 上传到本地服务器
            // $param['push_uidup_logoff_file'] = \think\facade\Filesystem::disk('public')->putFile( 'topic', $file);
            $array = $image->handle($param['push_uidup_logoff_file'], 'push_uidup_logoff_file', 'push_uidup_logoff_file_name');
            $param['push_uidup_logoff_file'] = $array['push_uidup_logoff_file'];
            $param['push_uidup_logoff_file_name'] = $array['push_uidup_logoff_file_name'];
        }
        Db::startTrans();
        try {
            //获取第一步资料
            $data = $chargeback->getNotifierGetUidModel($uid, $param['certificate_code'], $param['order_company_id']);

            //查询此证书是倒证还是证书注册
            $cerData = $company->getIdCertificateCodeFindModel($data['order_company_id'], $data['certificate_code']);

            if (empty($cerData)) {
                $btype = 2;
                $cerData = $exchangeModel->getWhereIdOrderByCreateData($param['order_company_id']);
//                if ($cerData['status'] < 3 && $cerData['status'] != 0) return ReJson (message::$ERROR_CODE,message::$ORDER_CERTIFICATE_NO);

//                //注销
                $exchangeModel->setLogOffModel($cerData['id'], $cerData['certificate_code']);

                //添加记录
                $orderLog->addDataModel([
                    'submit_id' => $request->param('user_login_id'),
                    'message' => message::$ORDER_CERTIFICATE_LOG_OFF,
                    'content' => '',
                    'type' => 2,
                    'order_company_id' => $cerData['id'],
                    'certificate_code' => $cerData['certificate_code']
                ]);

                //通过证书编号查询人才身份证
                $idCard = $exchangeModel->getCerIdcardFindModel($cerData['id'], $cerData['certificate_code']);

                //注销以后该证书修改为优选证书 //注销以后该证书修改为待审核 废弃
                //获取证书专业ID
                $cerWhere = '';
                if ($cerData['certificate_id']) {
                    if (isset($cerData['certificate_id'][3])) {
                        $cerWhere = ['certificate_zhuanye' => $cerData['certificate_id'][3]];
                    } else if (isset($cerData['certificate_id'][2])) {
                        $cerWhere = ['certificate_dengji' => $cerData['certificate_id'][2]];
                    } else if (isset($cerData['certificate_id'][1])) {
                        $cerWhere = ['certificate_leibie' => $cerData['certificate_id'][1]];
                    } else {
                        $cerWhere = ['certificate_leixing' => $cerData['certificate_id'][0]];
                    }
                }

                //修改人才信息为已退单person_id_card
                if ($cerWhere) {
                    $certificateRegister->becomeLogOffModel($cerWhere, $idCard);
                }
                //修改人才为已注销
                if ($idCard) {
                    $personModel->setIdCardStatus($idCard, 5);
                }

                /**
                 * 修改缓存
                 */
                $redisService->delOrderDzData ($cerData, "order_dz");
                //删除业绩总表缓存
                $redisService->delPerformance ($cerData);

            } else {
                $btype = 3;
//                if ($cerData['status'] < 3 && $cerData['status'] != 0) return ReJson (message::$ERROR_CODE,message::$ORDER_CERTIFICATE_NO);

//                //注销
                $company->setLogOffModel($cerData['id'], $cerData['certificate_code']);
                //添加记录

                $orderLog->addDataModel([
                    'submit_id' => $request->param('user_login_id'),
                    'message' => message::$ORDER_CERTIFICATE_LOG_OFF,
                    'content' => '',
                    'type' => 2,
                    'order_company_id' => $cerData['id'],
                    'certificate_code' => $cerData['certificate_code']
                ]);
                //通过证书编号查询人才身份证
                $idCard = $company->getCerIdcardFindModel($cerData['id'], $cerData['certificate_code']);
                //注销以后该证书修改为优选证书 //注销以后该证书修改为待审核

                //获取证书专业ID
                if (isset($cerData['certificate_id'][3])) {
                    $cerWhere = ['certificate_zhuanye' => $cerData['certificate_id'][3]];
                } else if (isset($cerData['certificate_id'][2])) {
                    $cerWhere = ['certificate_dengji' => $cerData['certificate_id'][2]];
                } else if (isset($cerData['certificate_id'][1])) {
                    $cerWhere = ['certificate_leibie' => $cerData['certificate_id'][1]];
                } else {
                    $cerWhere = ['certificate_leixing' => $cerData['certificate_id'][0]];
                }

                //修改人才信息为已退单person_id_card
                $certificateRegister->becomeLogOffModel($cerWhere, $idCard);
                //修改人才为已注销
                $personModel->setIdCardStatus($idCard, 5);
                //修改缓存
                $redisService->updateOrderZsshData ($certificateRegisterModel->getPersonIdCardOrderModel ($idCard)['id']);

                $redisService->delPersonData ($personModel->getPersonIdCardInfo ($idCard)['id']);
                /**
                 * 修改缓存
                 */
                $redisService->delOrderDzData ($cerData, "order_gszs");
                //删除业绩总表缓存
                $redisService->delPerformance ($cerData);
            }


            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return ReJson(message::$ERROR_CODE, $e->getMessage());
        }
        $oid = $cerData['id'];
        //发起注销操作
        $chargeback->editCerDataModel($param['certificate_code'], $param);

        //添加信息列表
        $messages = $message->addLogOffModel($cerData['submit_id'], $param['certificate_code'], $uid, $btype, $oid, '');
        //发送消息 //发送给订单发起人
        // $gateway->sendToUid($cerData['submit_id'],json_encode ($messages));
        $systemOperateLogModel->addOperate ($uid,message::$ORDER,'注销完成'.$param['certificate_code']);

        return ReJson(message::$SUCC_CODE, message::$SUCC_CERTIFICATE_LOG_OFF);
    }

    //获取退单|注销第一步资料
    public function getOpInformation(Request $request, OrderDealChargeback $chargeback, Image $image)
    {
        $param = $request->get();
        $uid = $request->get('user_login_id');
        if (empty($param['certificate_code']) || empty($param['order_company_id'])) return ReJson(message::$ERROR_CODE, message::$ERR_PARAM_EMPTY);

        $result = $chargeback->getNotifierGetUidModel($uid, $param['certificate_code'], $param['order_company_id']);
        if (!$result){
            return ReJson(message::$SUCC_CODE, message::$SUCC_SELECT_DATA, []);
        }
        $result = $image->handleList($result, ['push_uidup_logoff_file', 'get_uid_up_logoff_file']);

        if ($result) {
            if (isset($result['notifier_get_uid'])) {
                $result['notifier_get_uid'] = [
                    'id' => $result['notifier_get_uid'],
                    'text' => (new User())->getIdNickName($result['notifier_get_uid']) ?? ''
                ];
            } else {
                $result['notifier_get_uid'] = [
                    'id' => '',
                    'text' => ''
                ];
            }
        }

        return ReJson(message::$SUCC_CODE, message::$SUCC_SELECT_DATA, $result);
    }

    //续签
    //获取续签订单的详情内容
    public function getRenewalOrder(
        Request                            $request,
        OrderEnterpriseCompany             $company,
        OrderEnterpriseCertificateRegister $register,
        OrderEnterpriseExchange            $exchangeModel,
        Image                              $image,
        OrderDeal                          $dealModel,
        RedisService $redisService
    ){
        $id = $request->get('id');
        $certificateCode = $request->get('certificate_code');

        if (empty($id) || empty($certificateCode)) return ReJson(message::$ERROR_CODE, message::$ERR_PARAM_EMPTY);

//        $result = $company->getCertificateCodeFindDataModel($id,$certificateCode);
        $result = Db::table('order_enterprise_company')
            ->alias('c')
            ->join('order_enterprise_certificate_register m', 'm.certificate_code = c.enter_certificate_code')
            ->where(['c.id' => $id, 'c.certificate_code' => $certificateCode])
            ->field('c.*,
            m.business_id,
            m.sale_org,
            m.sale_uid,
            m.reg_unit,
            m.price_unit,
            m.certificate_zhuanye,
            m.certificate_dengji,
            m.certificate_leibie,
            m.certificate_leixing,
            m.certificate_id,
            m.people_num,
            m.project,
            m.cycle,
            m.social_security,
            m.certificate_b'
            )
            ->find();

        //拼接公示资料
        $pubData = $dealModel->getOrderCompanyIdFindModel($result['id'], $result['certificate_code']);
        if (!empty($pubData)) {
            $result = array_merge($pubData, $result);
        }

        if (empty($result)) {
//            $result = $exchangeModel->getFinanceFieldFindModel($certificateCode);
            $result = Db::table('order_enterprise_exchange')
                ->where(['certificate_code' => $certificateCode])
                ->find();
            if (!empty($result)) {
                //拼接公示资料
                $pubData = $dealModel->getOrderCompanyIdFindModel($result['id'], $result['certificate_code']);
                if (!empty($pubData)) {
                    $result = array_merge($pubData, $result);
                }
                $filed = ['info_file', 'reg_file'];
                $result = $image->handleList($result, $filed);
                $result = $image->handleUrl($result, $filed);
            }

        } else {
            $filed = ['reg_from_file'];
            $result = $image->handleList($result, $filed);
            $result = $image->handleUrl($result, $filed);
        }
        return ReJson(message::$SUCC_CODE, message::$SUCC_SELECT_DATA, $result);
    }

    //撤销退单
    public function revokeChargeback(
        Request                            $request,
        OrderEnterpriseCertificateBusiness $business,
        OrderOtherCertificateBusiness      $otherCerBusiness,
        OrderEnterpriseCompany             $company,
        OrderEnterpriseExchange            $exchange,
        OrderEnterpriseCertificateRegister $orderEnterpriseCertificateRegisterModel,
        OrderDealChargeback                $chargebackModel,
        \app\model\UserMessage             $message,
        Gateway                            $gateway,
        OrderLog                           $orderLog,
        OrderPersonCertificateRegister     $OrderPersonCertificateRegister,
        CluePerson                         $personModel,
        SystemOperateLog $systemOperateLogModel,
        RedisService $redisService
    ){
        $param = $request->post();
        if (empty($param['order_company_id']) || empty($param['certificate_code'])) {
            return ReJson(message::$ERROR_CODE, message::$ERR_PARAM_EMPTY);
        }
        $uid = $param['user_login_id'];
        $certificateCode = $param['certificate_code'];
        $order_company_id = '';
        try {
            /**
             * 先查询该条退单
             */
            $cData = $chargebackModel->getCidCerDataModel ($param['order_company_id'], $param['certificate_code']);
            //删除退单操作
            $chargebackModel->delCerIdDataModel($param['order_company_id'], $param['certificate_code']);
            //更新报表中心业绩统计
            $redisService->updateDateBetweenData($cData['charge_back_date'],$cData['charge_back_date']);//新加

            //查询是否是企业证书成交
            if (!empty($business->getCerFieldDataModel($certificateCode))) {

                //查看订单详情
                $orderData = $business->getCerDataModel($certificateCode);
                $order_company_id = $orderData['id'];
                if ($orderData['status']['id'] === 2) { //已退单
                    $orderStatus = 1;
                    //查询订单操作记录 修改为上一条状态
                    $orderDatas = $orderLog->getOrderLogModel($param['order_company_id'], $param['certificate_code']);

                    if ($orderDatas) {
                        foreach ($orderDatas as $orderDatum) {

                            if ($orderDatum['message'] == '生产完成') {
                                $orderStatus = 1;
                                break;
                            } else if ($orderDatum['message'] == '生产中') {
                                $orderStatus = 12;
                                break;
                            } else if ($orderDatum['message'] == '录单中') {
                                $orderStatus = 11;
                                break;
                            }
                        }
                        //修改订单状态
                        $business->setStatusBack($certificateCode, $orderStatus);

                    }
                }
            }

            //查询是否是证书办理成交
            if (!empty($otherCerBusiness->getFinanceFieldFindModel($certificateCode))) {

                //查看订单是否为已完成订单
                $orderData = $otherCerBusiness->getCerDataModel($certificateCode);
                $order_company_id = $orderData['id'];

                if ($orderData['status']['id'] === 2) { //已退单
                    $orderStatus = 1;
                    //查询订单操作记录 修改为上一条状态
                    $orderDatas = $orderLog->getOrderLogModel($param['order_company_id'], $param['certificate_code']);
                    if ($orderDatas) {
                        foreach ($orderDatas as $orderDatum) {
                            if ($orderDatum['message'] == '生产完成') {
                                $orderStatus = 1;
                                break;
                            } else if ($orderDatum['message'] == '生产中') {
                                $orderStatus = 12;
                                break;
                            } else if ($orderDatum['message'] == '录单中') {
                                $orderStatus = 11;
                                break;
                            }
                        }
                        //修改订单状态
                        $otherCerBusiness->setStatusBack($certificateCode, $orderStatus);

                    }
                }
            }


            //查询是否是证书注册 证书注册不可以撤销退单
            if (!empty($company->getCertificateCodeFindModel($certificateCode))) {

            }

            //查询是否是倒证成交
            if (!empty($exchange->getFinanceFieldFindModel($certificateCode))) {

                //查看订单是否为已完成订单
                $orderData = $exchange->getFinanceFieldFindModel($certificateCode);
                $order_company_id = $orderData['id'];

                if ($orderData['status']['id'] === 3) { //已退单
                    $orderStatus = 1;
                    //查询订单操作记录 修改为上一条状态
                    $orderDatas = $orderLog->getOrderLogModel($param['order_company_id'], $param['certificate_code']);
                    if ($orderDatas) {

                        foreach ($orderDatas as $orderDatum) {
                            if ($orderDatum['message'] == '证书注册完成') {
                                $orderStatus = 2;
                                break;
                            } else if ($orderDatum['message'] == '已售出') {
                                $orderStatus = 1;
                                break;
                            }
                        }
                        //修改订单状态
                        $exchange->setStatusBack($certificateCode, $orderStatus);
                        /**
                         * 修改订单缓存状态
                         */
                        $redisService->delOrderDzData ($orderData, "order_dz");
                        //删除业绩总表缓存
                        $redisService->delPerformance ($orderData);
                    }
                }

            }

            //添加日志
            $orderLog->addDataModel([
                'submit_id' => $uid,
                'message' => message::$ORDER_CHEXIAO_TUIDAN,
                'type' => 1,
                'order_company_id' => $order_company_id,
                'certificate_code' => $param['certificate_code']
            ]);

            $systemOperateLogModel->addOperate ($uid,message::$ORDER,'撤销退单'.$param['certificate_code']);
        } catch (ValidateException|\Exception $e) {
            return ReJson(message::$ERROR_CODE, $e->getMessage());
        }
        return ReJson(message::$SUCC_CODE, message::$SUCC_REVOKE_TUIDAN);
    }


    /****************************************************************************************财务管理*************************************************************/

    //财务总表
    public function getPerformanceList(
        Request       $request,
        OrderDeal     $deal,
        FinanceServer $financeServer,
        Power         $power,
        Certificate   $certificateSer,
        OrderOtherCertificateBusiness $otherCertificateBusinessModel,
        OrderEnterpriseCertificateBusiness $orderEnterpriseCertificateBusinessModel,
        RedisService $redisService,
        System $systemSer,
        UserOrganization $organizationModel
    ){
        $aliasId = $request->get('aliasId');
        $page = $request->get('page') ?: 1;
        $pageNum = $request->get('pageNum') ?: stateData::$_page;
        $param = $request->get();
        $uid = $request->param('user_login_id');
        //根据条件查询用户范围
        $userIdData = $power->getUserDataRange($uid);

        //字段权限
        $fieldListTree = $power->getUserFieldPower($aliasId,$uid);

        //拼接状态字段检索值
        foreach ($fieldListTree as &$item) {
            if ($item['alias_field_id'] == 'status') {
                $item['type'] = 'select';
                $item['value'] = [
                    [
                        'id' => '生产中',
                        'text' => '生产中',
                    ],
                    [
                        'id' => '录单中',
                        'text' => '录单中',
                    ],
                    [
                        'id' => '生产完成',
                        'text' => '生产完成',
                    ],
                    [
                        'id' => '已售出',
                        'text' => '已售出',
                    ],
                    [
                        'id' => '证书注册完成',
                        'text' => '证书注册完成',
                    ],
                    [
                        'id' => '已续签',
                        'text' => '已续签',
                    ],
                    [
                        'id' => '已退单',
                        'text' => '已退单',
                    ],
//                    [
//                        'id'=>'退单完成',
//                        'text'=>'退单完成',
//                    ],
                ];
            }
        }

        //查询所有成交过的数据 倒证&公司证书
//        $dealData = $deal->getOrderList();


        /**
         * 原业绩总表 逻辑正确 加载缓慢
         */
//        if (!empty($dealData)) {
//            //获取各业务分类详情
//            $result = $financeServer->getEachDetails($dealData, $userIdData);
//
//            //处理搜索条件
//            //业绩总表status删除 更改为cw_status 搜索
//            $param['cw_status'] = $param['status'] ?? '';
//            $param['status'] = '';
//
//            $data = $financeServer->resultSearchHandle($param, $result);
//
//            $pages['total'] = count($data);
//            $pages['pageNum'] = $pageNum;
//        }

        //获取订单id cer saleDate 订单集合
        $dataId = $financeServer->getOrderDataIdData($userIdData);

        /**
         * 获取缓存
         */
        $data = $redisService->getPerformanceList($dataId,$userIdData,$uid);

        //处理搜索条件
        //业绩总表status删除 更改为cw_status 搜索
        $param['cw_status'] = $param['status'] ?? '';
        $param['status'] = '';

        $data = $financeServer->resultSearchHandle($param, $data);
        /**
         * 列表上方公司部门 人员筛选 ||条件
         * 搜索设计全局没有 或者 条件
         */
        $orgData = $organizationModel->getStructure();
        
        if (isset($param['input_org']) && $param['input_org']){  //接收值ID
            unset($item);
            foreach ($data as $ke => $item) {
                if (isset($param['input_uid']) && $param['input_uid']){ //接受值TEXT
                    if (stripos($item['out_uid']['text'],$param['input_uid']) === false && stripos($item['get_uid']['text'],$param['input_uid']) === false){
                        unset($data[$ke]);
                        continue;
                    }
                }
                $orgArr = explode(',', rtrim($systemSer->getOrgIdStr($param['input_org'], $orgData), ','));

                if (!in_array($item['out_org']['id'], $orgArr) && !in_array($item['get_org']['id'] , $orgArr)) {
                    unset($data[$ke]);
                }

            }
        }
        $data = array_values ($data);

        /**
         * 计算合计  业绩  收证价 出证价 已付款  已收款 退单业绩
         */
        $sumData = [
            'id'=>'',
            'certificate_code'=>'总计',
            'performance'=>0,//业绩
            'sale_date'=>'',
            'business_id'=>'',
            'order_status'=>'',
            'person_name'=>'',
            'certificate_leixing'=>'',
            'certificate_leibie'=>'',
            'certificate_dengji'=>'',
            'certificate_zhuanye'=>'',
            'get_org'=>'',
            'get_uid'=>'',
            'out_org'=>'',
            'out_uid'=>'',
            'get_price_date'=>'',
            'out_price_date'=>'',
            'status'=>'',
            'td_date'=>'',
            'sfk_status'=>'',
            'sfk_wc_date'=>'',
            'ckjl'=>'',
            'label'=>'',
            'sale_price'=>0,//收证价
            'contract_price'=>0,//出证价
            'out_price_all'=>0,//已付款
            'get_price_all'=>0,//已收款
            'td_performance'=>0 //退单业绩
        ];

        foreach ($data as $k => $value) {
            if (empty($value)){
                continue;
            }
            if (isset($value['childs'])){

                foreach ($value['childs'] as $child) {
                    $sumData['performance'] += (int)$child['performance'];
                    $sumData['sale_price'] += (int)$child['sale_price'];
                    $sumData['contract_price'] += (int)$child['contract_price'];
                    $sumData['out_price_all'] += (int)$child['out_price_all'];
                    $sumData['get_price_all'] += (int)$child['get_price_all'];
                    $sumData['td_performance'] += (int)($child['td_performance']??0);
                }
            }

            $sumData['performance'] += (int)$value['performance'];
            $sumData['sale_price'] += (int)$value['sale_price'];
            $sumData['contract_price'] += (int)$value['contract_price'];
            $sumData['out_price_all'] += (int)$value['out_price_all'];
            $sumData['get_price_all'] += (int)$value['get_price_all'];
            $sumData['td_performance'] += (int)($value['td_performance']??0);

        }

        $pages['total'] = count ($data);
        $pages['pageNum'] = $pageNum;

        $pageData = page($data, $page, $pageNum,'desc','sale_date');
        //合计
        $pageData[] = $sumData;

        return ReJson(message::$SUCC_CODE, message::$SUCC_SELECT_DATA, [
            //结果分页
            'data' => $pageData,
            'power' => $fieldListTree
        ], $pages);
    }

    //查看业务详情
    public function getBusinessFinanceDetails(
        Request                       $request,
        CluePerson                    $person,
        ClueMobile                    $mobModel,
        ClueCommunicateRecord         $comRecord,
        CluePersonCertificate         $certifiModel,
        CluePersonAllocationRecord    $allocationRe,
        Certificate                   $certificate,
        OrderEnterpriseCompany        $company,
        OrderEnterpriseExchange       $exchange,
        OrderOtherCertificateBusiness $other,
        Power                         $power,
        FinanceServer                 $financeServer,
        OrderTransactionLog           $orderTransactionLogModel
    ){
        $param = $request->get();

        if (empty($param['certificate_code']) || empty($param['id'])) return ReJson(message::$ERROR_CODE, message::$ERR_PARAM_EMPTY);

        //证书详情
        $result['data'] = $financeServer->getBusinessDetails($param['id'], $financeServer->getCertificateType($param['certificate_code']), $param['certificate_code']);

        //收款信息
        $result['collection_data'] = $financeServer->getCollectionPaymentList($param['id'], $param['certificate_code'], $this->Collection);
        //付款信息
        $result['payment_data'] = $financeServer->getCollectionPaymentList($param['id'], $param['certificate_code'], $this->Payment);

        //订单流程
//        $result['order_log'] = $orderLog->getIdDataModel($param['id'],$financeServer->getCertificateType($param['certificate_code'])+1);

        //收付款日志
        if (!empty($result['collection_data'])) {
            $result['transaction_log'] = $orderTransactionLogModel->getIdDataModel($result['collection_data'][0]['id']);
        } else {
            $result['transaction_log'] = [];
        }

        //订单字段模板
        $zszcField = $power->getUserFieldPower('ddzszc', $request->param('user_login_id'));//证书注册
        $dzField = $power->getUserFieldPower('ddzszcdz', $request->param('user_login_id'));//倒证
        $zsblField = $power->getUserFieldPower('ddzsbl', $request->param('user_login_id'));//证书办理

        //查询人才关联订单
        //先查询企业证书注册订单
        $comData = $company->getPersonIdCardListModel($result['data']['person_id_card']);
        //给所有证书注册添加字段
        $data = [];
        foreach ($comData as $key => &$comDatum) {
            //ddzszc
            foreach ($comDatum as $k => $item) {

                //如果是获取器数组 则只去text
                if (is_array($item)) {
                    if (isset($item['text'])) {
                        $item = $item['text'] ?: '';
                    }
                }

                $data[$key]['id'] = $comDatum['id'];
                $data[$key]['bh'] = $comDatum['certificate_code'];
                $data[$key]['create_date'] = $comDatum['create_date'];
                $data[$key]['leixing'] = '证书注册';
//                print_r ($k);
//                echo PHP_EOL;
                if ($k == 'status') {
                    $data[$key]['status'] = $item;
                }
                foreach ($zszcField as $zszciItem) {
                    //如果模板字段等于订单详情字段 拼接字段name
                    if ($k == $zszciItem['alias_field_id']) {
                        if (!empty($zszciItem['name'])) {
                            $data[$key]['info'][] = ['id' => $zszciItem['name'], 'text' => $item];
                        }
                    }
                }
            }
        }

        //查询倒证订单
        $exData = $exchange->getExchangeIdCardListModel($result['data']['person_id_card']);

        $dzData = [];
        foreach ($exData as $keys => &$exDatum) {
            //ddzszcdz
//            $exDatum['leixing'] =  '倒证';
            foreach ($exDatum as $ks => $items) {
                //如果是获取器数组 则只去text
                if (is_array($items)) {
                    if (isset($items['text'])) {
                        $items = $items['text'] ?: '';
                    }
                }
                $dzData[$keys]['id'] = $exDatum['id'];
                $dzData[$keys]['bh'] = $exDatum['certificate_code'];
                $dzData[$keys]['create_date'] = $exDatum['create_date'];
                $dzData[$keys]['leixing'] = '证书注册';

                if ($ks == 'status') {
                    $dzData[$keys]['status'] = $items;
                }
                foreach ($dzField as $dzItem) {
                    //如果模板字段等于订单详情字段 拼接字段name
                    if ($ks == $dzItem['alias_field_id']) {
                        if (!empty($dzItem['name'])) {
                            $dzData[$keys]['info'][] = ['id' => $dzItem['name'], 'text' => $items];
                        }
                    }
                }
            }
        }

        //查询证书办理
        $otherData = $other->getIdCardDataModel($result['data']['person_id_card']);

        $zsblData = [];
        foreach ($otherData as $keyss => &$otDatum) {
            //ddzsbl
//            $otDatum['leixing'] =  '证书办理';
            foreach ($otDatum as $kss => $itemss) {
                //如果是获取器数组 则只去text
                if (is_array($itemss)) {
                    if (isset($itemss['text'])) {
                        $itemss = $itemss['text'] ?: '';
                    }
                }
                $zsblData[$keyss]['id'] = $otDatum['id'];
                $zsblData[$keyss]['bh'] = $otDatum['certificate_code'];
                $zsblData[$keyss]['create_date'] = $otDatum['create_date'];
                $zsblData[$keyss]['leixing'] = '证书注册';

                if ($kss == 'status') {
                    $zsblData[$keyss]['status'] = $itemss;
                }

                foreach ($zsblField as $zsblItem) {
                    //如果模板字段等于订单详情字段 拼接字段name
                    if ($kss == $zsblItem['alias_field_id']) {

                        if (!empty($zsblItem['name'])) {
                            $zsblData[$keyss]['info'][] = ['id' => $zsblItem['name'], 'text' => $itemss];

                        }
                    }
                }
            }

        }

        $result['order_log'] = array_merge($data, $dzData, $zsblData);
        return ReJson(message::$SUCC_CODE, message::$SUCC_SELECT_DATA, $result);
    }


    //查看业务详情
    public function getFindBusinessDetails(
        Request                            $request,
        CluePerson                         $person,
        ClueMobile                         $mobModel,
        ClueCommunicateRecord              $comRecord,
        CluePersonCertificate              $certifiModel,
        CluePersonAllocationRecord         $allocationRe,
        Certificate                        $certificate,
        OrderEnterpriseCompany             $company,
        OrderEnterpriseExchange            $exchange,
        OrderOtherCertificateBusiness      $other,
        OrderEnterpriseCertificateBusiness $OrderEnterpriseCertificateBusiness,
        Power                              $power,
        FinanceServer                      $financeServer,
        OrderTransactionLog                $orderTransactionLogModel,
        OrderTransactionRecord             $orderTransactionRecordModel
    ){
        $param = $request->get();

        if (empty($param['certificate_code']) || empty($param['id']) || empty($param['code'])) return ReJson(message::$ERROR_CODE, message::$ERR_PARAM_EMPTY);

        $type = $financeServer->getCertificateType($param['certificate_code']);

        //证书详情
        $result['data'] = $financeServer->getBusinessDetails($param['id'], $type, $param['certificate_code']);
        if (empty($result['data'])) {
            return ReJson(message::$ERROR_CODE, message::$SELECT_CERTIFICATE_ERR);
        }
        //收款信息
        $result['collection_data'] = $financeServer->getCollectionPaymentList($result['data']['id'], $param['certificate_code'], $this->Collection);
        //付款信息
        $result['payment_data'] = $financeServer->getCollectionPaymentList($result['data']['id'], $param['certificate_code'], $this->Payment);

        //订单流程
//        $result['order_log'] = $orderLog->getIdDataModel($param['id'],$financeServer->getCertificateType($param['certificate_code'])+1);

        //添加收付款日志
        //通过收付款编号查询收付款id
//        $tid = $orderTransactionRecordModel->getIdModel($param['code']);
//        $result['transaction_log'] = $orderTransactionLogModel->getIdDataModel ($tid);

        //订单字段模板
        $zszcField = $power->getUserFieldPower('ddzszccj', $request->param('user_login_id'));//证书注册
        $dzField = $power->getUserFieldPower('ddzszcdz', $request->param('user_login_id'));//倒证
        $zsblField = $power->getUserFieldPower('ddzsbl', $request->param('user_login_id'));//证书办理
        $qyzsField = $power->getUserFieldPower('ddqyzs', $request->param('user_login_id'));//企业证书
        $zsshField = $power->getUserFieldPower('ddzssh', $request->param('user_login_id'));

        //查询人才关联订单
        //先查询企业证书注册订单
        $comData = $company->getCerIdFindSelectModel($param['id'], $param['certificate_code']);
        //给所有证书注册添加字段
        $data = [];
        $businDatas = [];
        foreach ($comData as $key => &$comDatum) {
            //ddzszc
            foreach ($comDatum as $k => $item) {

                //如果是获取器数组 则只去text
                if (is_array($item)) {
                    if (isset($item['text'])) {
                        $item = $item['text'] ?: '';
                    }
                }

                $data[$key]['id'] = $comDatum['id'];
                $data[$key]['bh'] = $comDatum['certificate_code'];
                $data[$key]['create_date'] = $comDatum['create_date'];
                $data[$key]['leixing'] = '证书注册';
//                print_r ($k);
//                echo PHP_EOL;
                if ($k == 'status') {
                    $data[$key]['status'] = $item;
                }
                foreach ($zszcField as $zszciItem) {
                    //如果模板字段等于订单详情字段 拼接字段name
                    if ($k == $zszciItem['alias_field_id']) {
                        if (!empty($zszciItem['name'])) {
                            $data[$key]['info'][] = ['id' => $zszciItem['name'], 'text' => $item];
                        }
                    }
                }
            }
        }

        //查询倒证订单
        $exData = $exchange->getWhereIdDataSelectModel($param['id'], $param['certificate_code']);

        $dzData = [];
        foreach ($exData as $keys => &$exDatum) {
            //ddzszcdz
//            $exDatum['leixing'] =  '倒证';
            foreach ($exDatum as $ks => $items) {
                //如果是获取器数组 则只去text
                if (is_array($items)) {
                    if (isset($items['text'])) {
                        $items = $items['text'] ?: '';
                    }
                }
                $dzData[$keys]['id'] = $exDatum['id'];
                $dzData[$keys]['bh'] = $exDatum['certificate_code'];
                $dzData[$keys]['create_date'] = $exDatum['create_date'];
                $dzData[$keys]['leixing'] = '倒证';

                if ($ks == 'status') {
                    $dzData[$keys]['status'] = $items;
                }
                foreach ($dzField as $dzItem) {
                    //如果模板字段等于订单详情字段 拼接字段name
                    if ($ks == $dzItem['alias_field_id']) {
                        if (!empty($dzItem['name'])) {
                            $dzData[$keys]['info'][] = ['id' => $dzItem['name'], 'text' => $items];
                        }
                    }
                }
            }
        }
        //查询证书办理
        $otherData = $other->getWhereIdDataSelectModel($param['id'], $param['certificate_code']);
        //查询所有证书审核 person_id_card,ancillary_businesses_id
        $personReg = (new OrderPersonCertificateRegister)->getAncillaryIdNotEmptyModel();
        $personModel = new OrderPersonCertificateRegister();
        $zsblData = [];
        foreach ($otherData as $keyss => &$otDatum) {

            //ddzsbl
            foreach ($otDatum as $kss => $itemss) {
                //如果是获取器数组 则只去text
                if (is_array($itemss)) {
                    if (isset($itemss['text'])) {
                        $itemss = $itemss['text'] ?: '';
                    }
                }
                $zsblData[0]['id'] = $otDatum['id'];
                $zsblData[0]['bh'] = $otDatum['certificate_code'];
                $zsblData[0]['create_date'] = $otDatum['create_date'];
                $zsblData[0]['leixing'] = '证书办理';

                if ($kss == 'status') {
                    $zsblData[0]['status'] = $itemss;
                }

                foreach ($zsblField as $zsblItem) {
                    //如果模板字段等于订单详情字段 拼接字段name
                    if ($kss == $zsblItem['alias_field_id']) {

                        if (!empty($zsblItem['name'])) {
                            $zsblData[0]['info'][] = ['id' => $zsblItem['name'], 'text' => $itemss];
                        }
                    }
                }
            }

            //如果当前订单为附属订单 关联查询主订单
            if ($otDatum['type'] == 1) {
                foreach ($personReg as $value) {
                    $ancillaryArr = explode(',', rtrim($value['ancillary_businesses_id'], ','));

                    if (in_array($otDatum['id'], $ancillaryArr)) { //订单id与附属id匹配

                        //通过idcard查询匹配订单
                        //判断该人才身份证是否已经出售
                        $comData = $company->getWhereIdCardFindData($value['person_id_card']);

                        if ($comData) {
                            unset($comDatum);
                            foreach ($comData as $ka => $comDatum) {
                                //如果是获取器数组 则只去text
                                if (is_array($comDatum)) {
                                    if (isset($comDatum['text'])) {
                                        $comDatum = $comDatum['text'] ?: '';
                                    }
                                }

                                $zsblData[1]['id'] = $comData['id'];
                                $zsblData[1]['bh'] = $comData['certificate_code'];
                                $zsblData[1]['create_date'] = $comData['create_date'];
                                $zsblData[1]['leixing'] = '证书注册';

                                if ($ka == 'status') {
                                    $zsblData[1]['status'] = $comDatum;
                                }

                                foreach ($zszcField as $zszciItem) {
                                    //如果模板字段等于订单详情字段 拼接字段name
                                    if ($ka == $zszciItem['alias_field_id']) {
                                        if (!empty($zszciItem['name'])) {
                                            $zsblData[1]['info'][] = ['id' => $zszciItem['name'], 'text' => $comDatum];
                                        }
                                    }
                                }
                            }
                        } else { //未出售查询证书审核信息
                            $personData = $personModel->getPersonIdCardOrderModel($value['person_id_card']);
                            foreach ($personData as $ka => $comDatum) {
                                //如果是获取器数组 则只去text
                                if (is_array($comDatum)) {
                                    if (isset($comDatum['text'])) {
                                        $comDatum = $comDatum['text'] ?: '';
                                    }
                                }

                                $zsblData[1]['id'] = $personData['id'];
                                $zsblData[1]['bh'] = $personData['certificate_code'];
                                $zsblData[1]['create_date'] = $personData['create_date'];
                                $zsblData[1]['leixing'] = '证书审核';

                                if ($ka == 'status') {
                                    $zsblData[1]['status'] = $comDatum;
                                }


                                foreach ($zsshField as $zsshItem) {
                                    //如果模板字段等于订单详情字段 拼接字段name
                                    if ($ka == $zsshItem['alias_field_id']) {
                                        if (!empty($zsshItem['name'])) {
                                            $zsblData[1]['info'][] = ['id' => $zsshItem['name'], 'text' => $comDatum];
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        //查询企业证书
        $businData = $OrderEnterpriseCertificateBusiness->getWhereIdDataSelectModel($param['id'], $param['certificate_code']);

        foreach ($businData as $keysssq => &$businDatum) {
            //ddzsbl
//            $otDatum['leixing'] =  '证书办理';
            foreach ($businDatum as $kssssss => $keysss) {
                //如果是获取器数组 则只去text
                if (is_array($keysss)) {
                    if (isset($keysss['text'])) {
                        $keysss = $keysss['text'] ?: '';
                    }
                }
                $businDatas[$keysssq]['id'] = $businDatum['id'];
                $businDatas[$keysssq]['bh'] = $businDatum['certificate_code'];
                $businDatas[$keysssq]['create_date'] = $businDatum['create_date'];
                $businDatas[$keysssq]['leixing'] = '企业证书';

                if ($kssssss == 'status') {
                    $businDatas[$keysssq]['status'] = $keysss;
                }

                foreach ($qyzsField as $qyzsItem) {
                    //如果模板字段等于订单详情字段 拼接字段name
                    if ($kssssss == $qyzsItem['alias_field_id']) {

                        if (!empty($qyzsItem['name'])) {
                            $businDatas[$keysssq]['info'][] = ['id' => $qyzsItem['name'], 'text' => $keysss];
                        }
                    }
                }
            }
        }


        $result['order_log'] = array_merge($data, $dzData, $zsblData, $businDatas);
        return ReJson(message::$SUCC_CODE, message::$SUCC_SELECT_DATA, $result);
    }

    //完成业务
    public function completeBusiness(
        Request                            $request,
        OrderDeal                          $deal,
        OrderLog                           $order_log,
        OrderEnterpriseCertificateRegister $enter,
        OrderPersonCertificateRegister     $person,
        OrderEnterpriseCertificateBusiness $business,
        OrderOtherCertificateBusiness      $other,
        OrderEnterpriseCompany             $company,
        OrderEnterpriseExchange            $exchange,
        CluePersonAllocationRecord         $personRecord,
        SystemOperateLog $systemOperateLogModel,
        RedisService $redisService
    ){
//        $id = $request->post('id');
        $uid = $request->post ('user_login_id');
//        $type = $request->post ('business_type'); //业务分类
//        $cer = $request->post('certificate_id'); //延伸id
        $param = $request->post ('param');
        if (empty($param)) {
            return ReJson(message::$ERROR_CODE, message::$ERR_PARAM_EMPTY);
        }

        Db::startTrans();
        try {
            foreach ($param as $item) {
                //成交完成
//            $deal->completeOp($id,$cer);
                //成交日志记录
                $order_log->addDataModel([
                    'submit_id' => $uid,
                    'message' => '订单完成',
                    'certificate_code' => $item['certificate_code'],
                    'order_company_id' => $item['id']
                ]);

                //订单完成
                //企业证书业务
                $reKey = '';
                $data = $business->getCerDataModel($item['certificate_code']);
                if (!empty($data)) {
                    $business->completeOrderSFKModel($item['id']);
                    $reKey = 'order_qyzs';
                    //删除订单缓存
                    $redisService->delOrderDzData ($data,$reKey);
                    //删除业绩总表缓存
                    $redisService->delPerformance ($data);
                }

                //证书办理
                $data = $other->getCerDataModel($item['certificate_code']);
                if (!empty($data)) {
                    $other->completeOrderSFKModel($item['id']);
                    $reKey = 'order_zsbl';
                    //删除订单缓存
                    $redisService->delOrderDzData ($data,$reKey);
                    //删除业绩总表缓存
                    $redisService->delPerformance ($data);
                }

                //查询是否是企业证书注册
                $data = $company->getCertificateCodeFindModel($item['certificate_code']);
                if (!empty($data)) {
                    $company->completeOrderSFKModel($item['id']);
                    $reKey = 'order_zszc';
                    //查询关联人才
//                $idcard = $company->getOrderPersonIdCardModel($id);
//                $personData = $person->getPersonIdCardOrderModel($idcard);


                    //修改人才状态
//                $person->completeOperateModel($personData['person_id_card']);

                    //添加人才生命周期
//                $personRecord->addPersonAllocationModel($personData['id'],message::$ORDER_PERSON_COMPLETE,$request->param ('user_login_id'));
                    //删除订单缓存
                    $redisService->delOrderDzData ($data,$reKey);
                    //删除业绩总表缓存
                    $redisService->delPerformance ($data);
                }

                //查询是否是倒证成交
                $data = $exchange->getFinanceFieldFindModel($item['certificate_code']);
                if (!empty($data)) {
                    $exchange->completeOrderSFKModel($item['id']);
                    $reKey = 'order_dz';
                    //删除订单缓存
                    $redisService->delOrderDzData ($data,$reKey);
                    //删除业绩总表缓存
                    $redisService->delPerformance ($data);
                }


                $systemOperateLogModel->addOperate ($uid,message::$ORDER,'收付款完成'.$item['certificate_code']);
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return ReJson(message::$ERROR_CODE, $e->getMessage());
        }

        return ReJson(message::$SUCC_CODE, message::$SUCC_OPERATE);
    }

    /**
     * 完成后点击返回修改状态 清空完成时间
     * @param Request $request
     * @param OrderDeal $deal
     * @param OrderLog $order_log
     * @param OrderEnterpriseCertificateRegister $enter
     * @param OrderPersonCertificateRegister $person
     * @param OrderEnterpriseCertificateBusiness $business
     * @param OrderOtherCertificateBusiness $other
     * @param OrderEnterpriseCompany $company
     * @param OrderEnterpriseExchange $exchange
     * @param CluePersonAllocationRecord $personRecord
     * @return false|string
     */
    public function backCompletionTime
    (
        Request                            $request,
        OrderDeal                          $deal,
        OrderLog                           $order_log,
        OrderEnterpriseCertificateRegister $enter,
        OrderPersonCertificateRegister     $person,
        OrderEnterpriseCertificateBusiness $business,
        OrderOtherCertificateBusiness      $other,
        OrderEnterpriseCompany             $company,
        OrderEnterpriseExchange            $exchange,
        CluePersonAllocationRecord         $personRecord,
        RedisService $redisService,
        SystemOperateLog $systemOperateLogModel,
        User $userModel
    )
    {
        $id = $request->post('id');
        $cer = $request->post('certificate_id'); //延伸id
        $uid = $request->post ('user_login_id');
        $nickName = $userModel->getIdNickName ($uid);

        if (empty($id) || empty($cer)) {
            return ReJson(message::$ERROR_CODE, message::$ERR_PARAM_EMPTY);
        }
        //成交日志记录
//        $order_log->addDataModel([
//            'submit_id' => $request->param('user_login_id'),
//            'message' => '订单完成',
//            'certificate_code' => $cer,
//            'order_company_id' => $id
//        ]);

        try {
            //完成后点击返回按钮
            $reKey = '';
            //企业证书业务
            $data = $business->getFinanceFieldFindModel($cer);
            if (!empty($data)) {
                $business->backCompleteOrderSFKModel($id);
                $reKey = 'order_qyzs';
                //删除订单缓存
                $redisService->delOrderDzData ($data,$reKey);
                //删除业绩总表缓存
                $redisService->delPerformance ($data);
            }

            //证书办理
            $data = $other->getFinanceFieldFindModel($cer);
            if (!empty($data)) {
                $other->backCompleteOrderSFKModel($id);
                $reKey = 'order_zsbl';
                //删除订单缓存
                $redisService->delOrderDzData ($data,$reKey);
                //删除业绩总表缓存
                $redisService->delPerformance ($data);
            }

            //查询是否是企业证书注册
            $data = $company->getCertificateCodeFindModel($cer);
            if (!empty($data)) {
                $company->backCompleteOrderSFKModel($id);
                $reKey = 'order_zszc';
                //删除订单缓存
                $redisService->delOrderDzData ($data,$reKey);
                //删除业绩总表缓存
                $redisService->delPerformance ($data);
            }
            //查询是否是倒证成交
            $data = $exchange->getFinanceFieldFindModel($cer);
            if (!empty($data)) {
                $exchange->backCompleteOrderSFKModel($id);
                $reKey = 'order_dz';
                //删除订单缓存
                $redisService->delOrderDzData ($data,$reKey);
                //删除业绩总表缓存
                $redisService->delPerformance ($data);
            }

            //添加系统日志
            $systemOperateLogModel->addOperate ($uid,message::$ORDER,'用户：'.$nickName.'撤销完成订单:'.$cer);
        } catch (\Exception $e) {
            Db::rollback();
            return ReJson(message::$ERROR_CODE, $e->getMessage());
        }

        return ReJson(message::$SUCC_CODE, message::$SUCC_OPERATE);
    }

    //删除操作
    /*public function delDeal(
        Request $request,
        OrderDeal $deal,
        OrderLog $order_log,
        OrderEnterpriseCertificateRegister $enter,
        OrderPersonCertificateRegister $person,
        OrderEnterpriseCertificateBusiness $business,
        OrderOtherCertificateBusiness $certificate,
        CluePersonAllocationRecord  $personRecord
    ){
       $id = $request->post ('id');
       if (empty($id)) ReJson (message::$ERROR_CODE,message::$ERR_PARAM_EMPTY);

       Db::startTrans();
        try {
            //查询要删除的成交信息
            $data = $deal->getIdFindModel($id);

            if ($data['type']== 1 || $data['type'] == 2){

                $enter->saveStatusInitModel($data['order_id']);
                //修改人才状态
                $person->saveStatusInitModel($data['extend_person_id']);
                //添加人才生命周期
                $personRecord->addPersonAllocationModel($data['extend_person_id'],message::$ORDER_PERSON_COMPLETE_DELETE,$request->param ('user_login_id'));
            }
            if ($data['type'] == 3){
                $business->saveStatusInitModel($data['order_id']);
            }

            if ($data['type'] == 4){
                $certificate->saveStatusInitModel($data['order_id']);
            }

            //成交日志记录
            $order_log->addDataModel([
                'submit_id'=>$request->param ('user_login_id'),
                'message'=>'删除成交信息',
                'type'=>$data['type'],
                'order_id'=>$data['order_id']
            ]);

            $deal->delOrderModel($id);

        }catch (\Exception $e){
            return ReJson (message::$ERROR_CODE,$e->getMessage ());
        }

        return ReJson (message::$SUCC_CODE,message::$SUCC_DATELE_DATA);
    }*/


    //发送催款信息
    public function pressMoneyInfo(
        Request                $request,
        \app\model\UserMessage $message,
        Gateway                $gateway,
        SystemOperateLog $systemOperateLogModel,
        RedisService $redisService,
        FinanceServer $financeServer,
        OrderEnterpriseCompany $enterpriseCompanyModel,
        OrderEnterpriseExchange $orderEnterpriseExchangeModel,
        OrderEnterpriseCertificateBusiness $orderEnterpriseCertificateBusinessModel,
        OrderOtherCertificateBusiness $otherCertificateBusinessModel
    ){
        $param = $request->post();
        $uid = $request->post('user_login_id');
        Db::startTrans();
        try {
            Validate(FinanceValidate::class)->check($param);

            $messages = $message->addPressForMoneyModel($param['uid'], $param['b_id'], $uid, $param['id']);
            $gateway->sendToUid($param['uid'], json_encode($messages));


            //判断是哪种类型证书
            $type = $financeServer->getCertificateType($param['b_id']);

            $data = [];
            $reKey = '';
            switch ($type) {
                case 1:
                    $data = $enterpriseCompanyModel->getFinanceFieldIdFindModel ($param['id'],$param['b_id']);
                    $reKey = 'order_gszs';
                    break;
                case 2:
                    $data = $orderEnterpriseExchangeModel->getWhereCerDataModel ($param['b_id']);
                    $reKey = 'order_dz';
                    break;
                case 3:
                    $data = $otherCertificateBusinessModel->getWhereIdDataModel ($param['id']);
                    $reKey = 'order_zsbl';
                    break;
                case 4:
                    $data = $orderEnterpriseCertificateBusinessModel->getWhereIdDataModel ($param['id']);
                    $reKey = 'order_qyzs';
                    break;
                default:
            }

            //删除订单缓存
//            $redisService->delOrderDzData ($data,$reKey);
            //删除业绩总表缓存
            $redisService->delPerformance ($data);
            $systemOperateLogModel->addOperate ($uid,message::$ORDER,'发起催款'.$param['b_id']);
            Db::commit();
        } catch (\Exception|ValidateException $e) {
            Db::rollback();
            return ReJson(message::$ERROR_CODE, $e->getMessage());
        }

        return ReJson(message::$SUCC_CODE, message::$SUCC_PRESSMONEY);
    }

    //修改业绩
    public function editDealPerformance(
        Request                            $request,
        OrderEnterpriseCompany             $company,
        OrderEnterpriseExchange            $exchange,
        OrderOtherCertificateBusiness      $other,
        OrderEnterpriseCertificateBusiness $business,
        FinanceServer                      $financeServer,
        SystemOperateLog $systemOperateLogModel
    ){
        $param = $request->post();
        $uid = $request->post ('user_login_id');

        if (empty($param['id']) || empty($param['performance']) || empty($param['certificate_code'])) return ReJson(message::$ERROR_CODE, message::$ERR_PARAM_EMPTY);

        try {
//            //判断是哪种类型证书
            $type = $financeServer->getCertificateType($param['certificate_code']);
//
//            switch ($type) {
//                case 1:
//                    $company->editIdPerformanceModel($param);
//                    break;
//                case 2:
//                    $exchange->editIdPerformanceModel($param);
//                    break;
//                case 3:
//                    $other->editIdPerformanceModel($param);
//                    break;
//                case 4:
//                    $business->editIdPerformanceModel($param);
//                    break;
//                default:
//            }
//            $systemOperateLogModel->addOperate ($uid,message::$ORDER,'业绩总表修改订单业绩'.$param['certificate_code']);
        } catch (\Exception $e) {

            return ReJson(message::$ERROR_CODE, $e->getMessage());
        }

        return ReJson(message::$SUCC_CODE, message::$SUCC_UPDATE);
    }

    //业绩总表添加订单备注
    public function setOrderNotes(
        Request                            $request,
        OrderEnterpriseCompany             $company,
        OrderEnterpriseExchange            $exchange,
        OrderOtherCertificateBusiness      $other,
        OrderEnterpriseCertificateBusiness $business,
        FinanceServer                      $financeServer,
        SystemOperateLog $systemOperateLogModel,
        RedisService $redisService
    ){
        $param = $request->post();
        $uid = $request->post ('user_login_id');

        if (empty($param['id']) || empty($param['certificate_code']) || empty($param['label'])) return ReJson(message::$ERROR_CODE, message::$ERR_PARAM_EMPTY);

        try {
            //判断是哪种类型证书
            $type = $financeServer->getCertificateType($param['certificate_code']);

            $data = [];
            $reKey = '';
            switch ($type) {
                case 1:
                    $company->editIdLabelModel($param);
                    $data = $company->getFinanceFieldIdFindModel ($param['id'],$param['certificate_code']);
                    $reKey = 'order_gszs';
                    break;
                case 2:
                    $exchange->editIdLabelModel($param);
                    $data = $exchange->getWhereCerDataModel ($param['certificate_code']);
                    $reKey = 'order_dz';
                    break;
                case 3:
                    $other->editIdLabelModel($param);
                    $data = $other->getWhereIdDataModel ($param['id']);
                    $reKey = 'order_zsbl';
                    break;
                case 4:
                    $business->editIdLabelModel($param);
                    $data = $business->getWhereIdDataModel ($param['id']);
                    $reKey = 'order_qyzs';
                    break;
                default:
            }
            //删除业绩总表缓存
            $redisService->delPerformance ($data);
            //删除订单缓存
            $redisService->delOrderDzData ($data,$reKey);
            $systemOperateLogModel->addOperate ($uid,message::$ORDER,'业绩总表-添加订单备注:'.$param['label'].'订单编号：'.$param['certificate_code']);
        } catch (\Exception $e) {

            return ReJson(message::$ERROR_CODE, $e->getMessage());
        }

        return ReJson(message::$SUCC_CODE, message::$SUCC_UPDATE);
    }

    //获取代收款证书注册订单列表
    public function getCerRegList(
        Request                 $request,
        OrderEnterpriseCompany  $company,
        OrderDeal               $deal,
        OrderEnterpriseExchange $exchange,
        OrderTransactionRecord  $transactionRecord,
        OrderDeal               $orderDeal
    ){
        $id = $request->get('id');
        $certificateCode = $request->get('certificate_code');
        $person_name = $request->get('person_name');//供证单位
        $employ_unit_title = $request->get('employ_unit_title');
        $uid = $request->get('user_login_id');

        if (empty($certificateCode) || empty($id)) return ReJson(message::$ERROR_CODE, message::$ERR_PARAM_EMPTY);

        if (empty($employ_unit_title)) return ReJson(message::$SUCC_CODE, message::$ERR_PARAM_EMPTY);

        //处理完数据
        $result = [];
        /**
         * 查询证书注册应收款列表
         */
        //查询企业证书注册 & 相关补充人才信息并成交过的数据

        //查询证书补充资料id
        $result['certificate_reg'] = $company->getCertificateCodeDataModel($certificateCode, '', $employ_unit_title);

//        if (!empty($companyData)){
//            //判断数据是否已经成交
//            foreach ($companyData as $companyDatum) {
//                if ($deal->getOrderCompanyIdFindModel($companyDatum['id'],$companyDatum['certificate_code'])){
//                    $result['certificate_reg'][] = $companyDatum;
//                }
//            }
//        }else{
//            $result['certificate_reg'] = [];
//        }
        //删除自己本条数据
        if (!empty($result['certificate_reg'])) {
            foreach ($result['certificate_reg'] as $key => $item) {
                if ($item['certificate_code'] == $certificateCode && $item['id'] == $id) {
                    unset($result['certificate_reg'][$key]);
                }
                //判断是否跟自己相关
                if ($item['get_uid']['id'] != $uid && $item['responsibility_uid'] != $uid && $item['submit_id'] != $uid) {
                    unset($result['certificate_reg'][$key]);
                }
            }
        }

        /**
         * 查询倒证应收列表
         */
        $result['certificate_exchange'] = $exchange->getCertificateCodeData($certificateCode, $person_name, $employ_unit_title);

//        if (!empty($exchangeData)){
//            //判断数据是否已经成交
//            foreach ($exchangeData as $exchangeDataDatum) {
//                if ($deal->getOrderCompanyIdFindModel($exchangeDataDatum['id'],$exchangeDataDatum['certificate_code'])){
//                    $result['certificate_exchange'][] = $exchangeDataDatum;
//                }
//            }
//        }else{
//            $result['certificate_exchange'] = [];
//        }

        //删除自己本条数据
        if (!empty($result['certificate_exchange'])) {
            foreach ($result['certificate_exchange'] as $key => $item) {
                if ($item['certificate_code'] == $certificateCode && $item['id'] == $id) {
                    unset($result['certificate_exchange'][$key]);
                    continue;
                }

                //判断是否跟自己相关
                if ($item['out_uid']['id'] != $uid && $item['get_uid']['id'] != $uid) {
                    unset($result['certificate_exchange'][$key]);
                }
            }
        }
        //证书注册应收应付
        if (isset($result['certificate_reg'])) {
            foreach ($result['certificate_reg'] as &$ite) {
                //拼接应收已收
                //            if ($ite['people_num'] == 0){
                //                $ite['people_num'] = 1;
                //            }
                $ite['receivable'] = round($ite['price_unit']);//应收
                $rece = $transactionRecord->getOrderStatusIdYsModel($ite['id'], $ite['certificate_code'])['all_price'];
                $ite['Received'] = $rece ?: 0; //已收
                //拼接应付已付
                $ite['yingfu'] = $ite['contract_price'];
                $yifu = $transactionRecord->getOrderStatusIdYsModel($ite['id'], $ite['certificate_code'], 1, 2)['all_price'];
                $ite['yifu'] = $yifu ?: 0; //已付
                $ite['get_price'] = $ite['contract_price'];

                //拼接公示资料
                $results = $orderDeal->getsuppleInfo($ite['id'], $ite['certificate_code']);
                $ite['electronic_certificate_file_illustrate'] = $results['electronic_certificate_file_illustrate'] ?: '';
                $ite['electronic_certificate_file'] = $results['electronic_certificate_file'] ?: '';
                $ite['electronic_certificate_date'] = $results['electronic_certificate_date'] ?: '';
                $ite['b_electronic_certificate_file_illustrate'] = $results['b_electronic_certificate_file_illustrate'] ?: '';
                $ite['b_electronic_certificate_date'] = $results['b_electronic_certificate_date'] ?: '';
                $ite['b_electronic_certificate_file'] = $results['b_electronic_certificate_file'] ?: '';
                $ite['b_reg_electronic_certificate_date'] = $results['b_reg_electronic_certificate_date'] ?: '';
                $ite['electronic_certificate_file_illustrate_name'] = $results['electronic_certificate_file_illustrate_name'] ?: '';
                $ite['b_electronic_certificate_file_name'] = $results['b_electronic_certificate_file_name'] ?: '';
                if (is_array($results['order_status'])) {
                    $ite['order_status'] = $results['order_status']['text'];
                } else {
                    $ite['order_status'] = $results['order_status'];
                }
            }
        }


        //倒证应收应付
        if (isset($result['certificate_exchange'])) {
            foreach ($result['certificate_exchange'] as &$items) {
                //拼接应收已收
                $items['receivable'] = $items['out_price'];//应收
                $rece = $transactionRecord->getOrderStatusIdYsModel($items['id'], $items['certificate_code'])['all_price'];
                $items['Received'] = $rece ?: 0; //已收
                //拼接应付已付
                $items['yingfu'] = $items['get_price'] ?: 0;
                $yifu = $transactionRecord->getOrderStatusIdYsModel($items['id'], $items['certificate_code'], 1, 2)['all_price']; //已付
                $items['yifu'] = $yifu ?: 0;
                $items['business_id'] = ['id' => '', 'text' => '倒证'];
                $items['sale_uid'] = $items['out_uid'];
                $items['sale_org'] = $items['out_org'];
                $items['reg_unit'] = $items['unit_reg_title'];
                $items['price_unit'] = $items['out_price'];
                $items['people_num'] = 0;
                $items['person_id_card'] = $items['id_card'];
                $items['reg_from_file_name'] = $items['reg_file_name'];

                //拼接公示资料
                $results = $orderDeal->getsuppleInfo($items['id'], $items['certificate_code']);
                $items['electronic_certificate_file_illustrate'] = $results['electronic_certificate_file_illustrate'] ?: '';
                $items['electronic_certificate_file'] = $results['electronic_certificate_file'] ?: '';
                $items['electronic_certificate_date'] = $results['electronic_certificate_date'] ?: '';
                $items['b_electronic_certificate_file_illustrate'] = $results['b_electronic_certificate_file_illustrate'] ?: '';
                $items['b_electronic_certificate_date'] = $results['b_electronic_certificate_date'] ?: '';
                $items['b_electronic_certificate_file'] = $results['b_electronic_certificate_file'] ?: '';
                $items['b_reg_electronic_certificate_date'] = $results['b_reg_electronic_certificate_date'] ?: '';
                $items['electronic_certificate_file_illustrate_name'] = $results['electronic_certificate_file_illustrate_name'] ?: '';
                $items['b_electronic_certificate_file_name'] = $results['b_electronic_certificate_file_name'] ?: '';
                if (is_array($results['order_status'])) {
                    $items['order_status'] = $results['order_status']['text'];
                } else {
                    $items['order_status'] = $results['order_status'];
                }
            }
        }


        if (isset($result['certificate_reg'], $result['certificate_exchange'])) {
            $arr = array_merge($result['certificate_exchange'], $result['certificate_reg']);
        } else if (isset($result['certificate_exchange'])) {
            $arr = $result['certificate_exchange'];
        } else if (isset($result['certificate_reg'])) {
            $arr = $result['certificate_reg'];
        }

        return ReJson(message::$SUCC_CODE, message::$SUCC_SELECT_DATA, array_values($arr));
    }

    //获取代收款证书办理订单列表
    public function getCerProcessing(Request $request, OrderDeal $deal, OrderOtherCertificateBusiness $processingModel, OrderTransactionRecord $transactionRecord)
    {
        $person_name = $request->get('person_name');
        $certificateCode = $request->get('certificate_code');
        $uid = $request->get('user_login_id');

        if (empty($person_name) || empty($certificateCode)) return ReJson(message::$SUCC_CODE, message::$ERR_PARAM_EMPTY);

        $result = $processingModel->getPersonNameModel($person_name);

        foreach ($result as $key => &$item) {
            //删除自己本条数据
            if ($item['certificate_code'] == $certificateCode) {
                unset($result[$key]);
                continue;
            }

            //判断是否跟自己相关
            if ($item['get_uid']['id'] != $uid) {
                unset($result[$key]);
                continue;
            }

            //拼接应收已收
            $item['receivable'] = $item['sale_price'];//应收
            $rece = $transactionRecord->getOrderStatusIdYsModel($item['id'], $item['certificate_code'])['all_price'];
            $item['Received'] = $rece ?: 0; //已收
            //拼接应付已付
            $item['yingfu'] = $item['cost_price'];
            $yifu = $transactionRecord->getOrderStatusIdYsModel($item['id'], $item['certificate_code'], 1, 2)['all_price']; //已付
            $item['yifu'] = $yifu ?: 0;
        }


        return ReJson(message::$SUCC_CODE, message::$SUCC_SELECT_DATA, array_values($result));
    }

    //获取代收款企业证书订单列表
    public function getEnterBusinessDeal(Request $request, OrderDeal $deal, OrderEnterpriseCertificateBusiness $businessModel, OrderTransactionRecord $transactionRecord)
    {
        $employ_unit_title = $request->get('enterprise_name');
        $certificateCode = $request->get('certificate_code');
        $uid = $request->get('user_login_id');

        if (empty($employ_unit_title) || empty($certificateCode)) return ReJson(message::$SUCC_CODE, message::$SELECT_ORDER_CERTIFICATE_EMPTY);

        $result = $businessModel->getEmployTitleDataModel($employ_unit_title);

        foreach ($result as $key => &$item) {
            //删除自己本条数据
            if ($item['certificate_code'] == $certificateCode) {
                unset($result[$key]);
                continue;
            }
            //判断是否跟自己相关
            if ($item['out_uid']['id'] != $uid && $item['get_uid']['id'] != $uid) {
                unset($result[$key]);
                continue;
            }

            //拼接应收已收
            $item['receivable'] = $item['sale_price'];//应收
            $rece = $transactionRecord->getOrderStatusIdYsModel($item['id'], $item['certificate_code'])['all_price'];
            $item['Received'] = $rece ?: 0; //已收

            //拼接应付已付
            $item['yingfu'] = $item['cost_price'];
            $yifu = $transactionRecord->getOrderStatusIdYsModel($item['id'], $item['certificate_code'], 1, 2)['all_price']; //已付
            $item['yifu'] = $yifu ?: 0;
        }

        return ReJson(message::$SUCC_CODE, message::$SUCC_SELECT_DATA, array_values($result));
    }

    /*************************************************************************************************
     * 收款单管理
     */
    //收款单列表
    public function collectionList(
        Request                $request,
        OrderTransactionRecord $transactionRecord,
        Power                  $power,
        \app\service\Order     $order,
        FinanceServer          $financeServer,
        RedisService $redisService
    ){
        //根据条件查询用户范围
        $aliasId = $request->get('aliasId');
        $uid = $request->get('user_login_id');
        $page = $request->get('page') ?: 1;
        $pageNum = $request->get('pageNum') ?: stateData::$_page;
        $param = $request->get();

        $id = $request->get('id');

        $userData = $power->getUserDataRange($uid);

        //字段权限
        $fieldListTree = $power->getUserFieldPower($aliasId, $request->param('user_login_id'));

        if ($id) {
            $dataId = $id;
        } else {
            $dataId = $transactionRecord->getTransactionListModel($this->Collection, $userData, $uid, $page, $pageNum);
        }
        $result = $redisService->getTransactionReList ($dataId);
//        $result = $order->addTransactionField($finData);

        if ($result) {
            foreach ($result as &$v) {
                $path_arr = explode(',', $v['annex']);
                $file_name_arr = explode(',', $v['annex_name']);
                $v['annex'] = [];
                foreach ($path_arr as $key => $value) {
                    $v['annex'][] = [
                        'file_name' => array_key_exists($key, $file_name_arr) ? $file_name_arr[$key] : '',
                        'path' => $value,
                    ];
                }
            }
        }

        //处理搜索值
        $data = $financeServer->resultSearchHandle($param, $result);
        $pages['total'] = count($data);
        $pages['pageNum'] = $pageNum;

        return ReJson(
            message::$SUCC_CODE,
            message::$SUCC_SELECT_DATA,
            [
                'data' => page($data, $page, $pageNum,'desc','submit_date'),
                'power' => $fieldListTree
            ], $pages
        );
    }

    //收款单详情
    public function collectionDetails(Request $request, OrderTransactionRecord $transactionRecord, FinanceServer $financeServer, OrderLog $orderLog)
    {
        $transaction_code = $request->get('transaction_code');

        if (empty($transaction_code)) return ReJson(message::$ERROR_CODE, message::$ERR_PARAM_EMPTY);

        $data = $transactionRecord->getTransactionCodeDataModel($transaction_code, $this->Collection);

        //拼接应收款已收款
        $result = $financeServer->ysPrice($data);

        sort($result);
        if ($result) {
            foreach ($result as &$v) {
                $path_arr = explode(',', $v['annex']);
                $file_name_arr = explode(',', $v['annex_name']);
                $v['annex'] = [];
                foreach ($path_arr as $key => $value) {
                    $v['annex'][] = [
                        'file_name' => array_key_exists($key, $file_name_arr) ? $file_name_arr[$key] : '',
                        'path' => $value,
                    ];
                }
            }
        }

//        print_r ($result);die;
//        $result['association'] = $financeServer->ysPrice($data['order_company_id'],$transactionRecord->getCertificateCodeDataModel($this->Collection,$data['certificate_code']));
//        $result['association'] = $transactionRecord->getCertificateCodeDataModel($this->Collection,$data['certificate_code']);

        //订单信息
//        $result['order_data'] = [$financeServer->getBusinessDetails ($data['id'],$financeServer->getCertificateType($data['certificate_code']),$data['certificate_code'])];

        //收款信息
//        $result['collection_data'] = $financeServer->getCollectionPaymentList($data['id'],$data['certificate_code'],$this->Collection);
        //付款信息
//        $result['payment_data'] = $financeServer->getCollectionPaymentList($data['id'],$data['certificate_code'],$this->Payment);

        //订单流程
//        $result['order_log'] = $orderLog->getIdDataModel($data['id'],$financeServer->getCertificateType($data['certificate_code'])+1);


        return ReJson(message::$SUCC_CODE, message::$SUCC_SELECT_DATA, $result);
    }

    //付款单列表
    public function paymentList(
        Request                $request,
        OrderTransactionRecord $transactionRecord,
        Power                  $power,
        \app\service\Order     $order,
        FinanceServer          $financeServer,
        OrderTransactionLog    $orderTransactionLogModel,
        RedisService $redisService
    ){
        //根据条件查询用户范围
        $param = $request->get();
        $aliasId = $request->get('aliasId');
        $uid = $request->param('user_login_id');
        $userData = $power->getUserDataRange($uid);
        $page = $request->param('page') ?: 1;
        $pageNum = $request->param('pageNum') ?: stateData::$_page;

        $id = $request->get('id');

        //字段权限
        $fieldListTree = $power->getUserFieldPower($aliasId, $uid);

        //拼接状态字段检索值
        foreach ($fieldListTree as &$item) {
            if ($item['alias_field_id'] == 'status') {
                $item['value'][] = [
                    'id'=>'4',
                    'text'=>'待付款'
                ];
            }
        }

        if ($id) {
            $dataId = $id;
        } else {
            $dataId = $transactionRecord->getTransactionListModel($this->Payment, $userData, $uid, $page, $pageNum);
        }

        $result = $redisService->getTransactionReList ($dataId);

        if (is_array ($result)) {
            foreach ($result as &$v) {
                $path_arr = explode(',', $v['annex']);
                $file_name_arr = explode(',', $v['annex_name']);
                $v['annex'] = [];
                foreach ($path_arr as $key => $value) {
                    $v['annex'][] = [
                        'file_name' => array_key_exists($key, $file_name_arr) ? $file_name_arr[$key] : '',
                        'path' => $value,
                    ];
                }
            }
             /**
             * 给前端拼接是否可以拒绝权限
             */
            foreach ($result as &$item) {
                if (!empty($item['sh_uid']['id'])) {
                    if ($uid == $item['sh_uid']['id']) {
                        $item['is_dels'] = 1;
                    } else {
                        $item['is_dels'] = 0;
                    }
                } else {
                    if (!empty($item['examine_uid']['id'])) {
                        if ($uid == $item['examine_uid']['id']) {
                            $item['is_dels'] = 1;
                        } else {
                            $item['is_dels'] = 0;
                        }
                    }
                }
            }
        }

        //搜索结果做搜索处理
        $data = $financeServer->resultSearchHandle($param, $result);
//        $pages['total'] = $transactionRecord->getTransactionCountModel($this->Payment, $userData, $uid);
        $pages['total'] = count ($data);
        $pages['pageNum'] = $pageNum;

        return ReJson(message::$SUCC_CODE, message::$SUCC_SELECT_DATA, [
            'data' => page($data, $page, $pageNum,'desc','transaction_date'),
            'power' => $fieldListTree
        ], $pages);
    }

    //付款单详情
    public function paymentDetails(
        Request                $request,
        OrderTransactionRecord $transactionRecord,
        FinanceServer          $financeServer,
        OrderLog               $orderLog,
        OrderTransactionLog    $orderTransactionLogModel,
        OrderTransactionRecord $orderTransactionRecordModel
    ){
        $uid = $request->get('user_login_id');
        $transaction_code = $request->get('transaction_code');

        if (empty($transaction_code)) return ReJson(message::$ERROR_CODE, message::$ERR_PARAM_EMPTY);
        $data = $transactionRecord->getTransactionCodeDataModel($transaction_code, $this->Payment);

        /**
         * 给前端拼接是否可以拒绝权限
         */
        if ($data){
            foreach ($data as &$item) {
                if (!empty($item['sh_uid']['id'])) {
                    if ($uid == $item['sh_uid']['id']) {
                        $item['is_dels'] = 1;
                    } else {
                        $item['is_dels'] = 0;
                    }
                } else {
                    if (!empty($item['examine_uid']['id'])) {
                        if ($uid == $item['examine_uid']['id']) {
                            $item['is_dels'] = 1;
                        } else {
                            $item['is_dels'] = 0;
                        }
                    }
                }
            }

            //拼接应付款款 已付款
            $result = $financeServer->ysPrice($data);
            if ($result) {
                foreach ($result as &$v) {
                    $path_arr = explode(',', $v['annex']);
                    $file_name_arr = explode(',', $v['annex_name']);
                    $v['annex'] = [];
                    foreach ($path_arr as $key => $value) {
                        $v['annex'][] = [
                            'file_name' => array_key_exists($key, $file_name_arr) ? $file_name_arr[$key] : '',
                            'path' => $value,
                        ];
                    }
                }
            }
        }

//        //收款信息 关联订单
//        $result['association'] = $transactionRecord->getCertificateCodeDataModel($this->Collection,$result['data']['certificate_code']);
//
//        //订单信息
//        $result['data'] = $financeServer->getBusinessDetails ($result['data']['id'],$financeServer->getCertificateType($result['data']['certificate_code']),$result['data']['certificate_code']);
//
//        //收款信息
//        $result['collection_data'] = $financeServer->getCollectionPaymentList($result['data']['id'],$result['data']['certificate_code'],$this->Collection);
//        //付款信息
//        $result['payment_data'] = $financeServer->getCollectionPaymentList($result['data']['id'],$result['data']['certificate_code'],$this->Payment);
//
//        //订单流程
//        $result['order_log'] = $orderLog->getIdDataModel($result['data']['id'],$financeServer->getCertificateType($result['data']['certificate_code'])+1);


        return ReJson(message::$SUCC_CODE, message::$SUCC_SELECT_DATA, $result);
    }

    //通过付款第一步 ()
    public function passTransactionOne(
        Request                $request,
        OrderTransactionRecord $transactionRecord,
        \app\model\UserMessage $message,
        Gateway                $gateway,
        OrderLog               $logModel,
        OrderTransactionLog    $orderTransactionLogModel,
        SystemOperateLog $systemOperateLogModel,
        RedisService $redisService
    ){
        $id = $request->post('id');
        $uid = $request->post('user_login_id');
        $shUid = $request->post('sh_uid');
        $code = $request->post('code');
        $refuse_text = $request->post('refuse_text');
        if (empty($id) || empty($shUid) || empty($code)) return ReJson(message::$ERROR_CODE, message::$ERR_PARAM_EMPTY);

        //通过code 查询收付款集合
        $data = $transactionRecord->getTransactionCodeDataModel($code, $this->Payment);
        if (!empty($data)) {
            Db::startTrans();
            try {
                $lid = 0;
                foreach ($data as $item) {
                    $transactionRecord->editDataModel($item['id'], [
                        'sh_uid' => $shUid,
                        'status' => 4, //代付款
                        'refuse_text'=>$refuse_text
                    ]);

                    /**
                     * 删除缓存
                     */
                    $redisService->delTransactionId($item['id']);

                    $lid = $item['id'];
                }
                //添加收付款日志
                $orderTransactionLogModel->createModel([
                    'tid' => $lid,
                    'content' => '付款流转',
                    'submit_id' => $uid,
                    'refuse_text'=>$refuse_text
                ]);

                $messages = $message->addPayMentModel($data[0]['transaction_code'], $shUid, $uid, $data[0]['id']);
                //发送消息
                $gateway->sendToUid($shUid, json_encode($messages));
                $systemOperateLogModel->addOperate ($uid,message::$ORDER,'流转收付款'.$data[0]['transaction_code']);

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return ReJson(message::$ERROR_CODE, $e->getMessage());
            }
        }
        return ReJson(message::$SUCC_CODE, message::$SUCC_OPERATE);
    }

    //通过收付款
    public function passTransaction(
        Request                $request,
        OrderTransactionRecord $transactionRecord,
        \app\model\UserMessage $message,
        Gateway                $gateway,
        OrderTransactionLog    $orderTransactionLogModel,
        SystemOperateLog $systemOperateLogModel,
        RedisService $redisService,
        OrderEnterpriseCompany $enterpriseCompanyModel,
        OrderEnterpriseExchange $orderEnterpriseExchangeModel,
        OrderEnterpriseCertificateBusiness $orderEnterpriseCertificateBusinessModel,
        OrderOtherCertificateBusiness $otherCertificateBusinessModel
    ){
        $id = $request->post('id');
        $uid = $request->post('user_login_id');
        $refuse_text = $request->post('refuse_text');
        if (empty($id)) return ReJson(message::$ERROR_CODE, message::$ERR_PARAM_EMPTY);

        Db::startTrans();
        try {
            //批量通过 id字符串转数组 循环操作
            $iaArr = explode(',', $id);

            //按订单集合来进行操作
            //通过id寻找收付款单号，通过单号找所有id
            $idArrs = [];
            $findBool = false;

            foreach ($iaArr as $item) {
                $find = $transactionRecord->getIdFindModel($item);
                $idArrs = $transactionRecord->getTransactionCodeCodeDataModel($find['transaction_code']);

                foreach ($idArrs as $items) {
                    $transactionRecord->passOpModel($items['id'],$refuse_text);

                    if (!$findBool) {
                        //添加消息列表
                        if ($find['transaction_type'] == 1) { //收款
                            $messages = $message->addCollectionPassModel($find['transaction_code'], $find['transaction_uid']['id'], $uid, $find['id']);
                            /**
                             * 删除缓存
                             */
                            $redisService->delFlowingId ($find['id']);
                            //添加收收款日志
                            $orderTransactionLogModel->createModel([
                                'tid' => $id,
                                'content' => '收款审核通过',
                                'submit_id' => $uid,
                                'refuse_text'=>$refuse_text
                            ]);
                            $redisService->delTransactionId ($id);
                        } else { //付款
                            $messages = $message->addPayMentPassModel($find['transaction_code'], $find['transaction_uid']['id'], $uid, $find['id']);
                            /**
                             * 删除缓存
                             */
                            $redisService->delFlowingId ($find['id']);
                            //添加收付款日志
                            $orderTransactionLogModel->createModel([
                                'tid' => $id,
                                'content' => '付款审核通过',
                                'submit_id' => $uid,
                                'refuse_text'=>$refuse_text
                            ]);
                            $redisService->delTransactionId ($id);
                        }

                        //发送消息
                        $gateway->sendToUid($find['transaction_uid']['id'], json_encode($messages));
                        $systemOperateLogModel->addOperate ($uid,message::$ORDER,'通过收付款'.$find['transaction_code']);
                        $findBool = true;
                    }

                    /**
                     * 删除财务管理-业绩总表缓存
                     */
                    //查询订单属于哪个订单 删除对应销售时间证书编号缓存
                    $saleData = $enterpriseCompanyModel->getCertificateCodeFindModel ($items['certificate_code']);
                    if ($saleData){ //公司证书
                        $redisService->delPerformance($saleData);
                    }

                    $saleData = $orderEnterpriseExchangeModel->getWhereCerDataModel ($items['certificate_code']);
                    if ($saleData){ //倒证
                        $redisService->delPerformance($saleData);
                    }

                    $saleData = $orderEnterpriseCertificateBusinessModel->getCerDataModel ($items['certificate_code']);
                    if ($saleData){ //企业证书
                        $redisService->delPerformance($saleData);
                    }

                    $saleData = $otherCertificateBusinessModel->getCerDataModel ($items['certificate_code']);
                    if ($saleData){ //证书办理
                        $redisService->delPerformance($saleData);
                    }
                }
            }


            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return ReJson(message::$ERROR_CODE, $e->getMessage());
        }
        return ReJson(message::$SUCC_CODE, message::$SUCC_OPERATE);
    }


    //拒绝收付款
    public function refuseTransaction(
        Request                $request,
        OrderTransactionRecord $transactionRecord,
        \app\model\UserMessage $message,
        Gateway                $gateway,
        OrderTransactionLog    $orderTransactionLogModel,
        SystemOperateLog $systemOperateLogModel,
        RedisService $redisService
    ){
        $id = $request->post('id');
        $uid = $request->post('user_login_id');
        $refuse_text = $request->post('refuse_text');
        if (empty($id)) return ReJson(message::$ERROR_CODE, message::$ERR_PARAM_EMPTY);

        Db::startTrans();
        try {

            //批量通过 id字符串转数组 循环操作
            $iaArr = explode(',', $id);

            //按订单集合来进行操作
            //通过id寻找收付款单号，通过单号找所有id
            $idArrs = [];
            $findBoole = false;
            foreach ($iaArr as $item) {
                $find = $transactionRecord->getIdFindModel($item);

                $idArrs = $transactionRecord->getTransactionCodeCodeDataModel($find['transaction_code']);

                foreach ($idArrs as $items) {
                    $transactionRecord->refuseOpModel($items['id'], $refuse_text);
                    if (!$findBoole) {
                        //添加消息列表
                        if ($find['transaction_type'] == 1) { //收款
                            $messages = $message->addCollectionRefuseModel($find['transaction_code'], $find['transaction_uid']['id'], $uid, $find['id']);
                            /**
                             * 删除缓存
                             */
                            $redisService->delFlowingId ($find['id']);
                            //添加收收款日志
                            $orderTransactionLogModel->createModel([
                                'tid' => $id,
                                'content' => '收款审核驳回',
                                'submit_id' => $uid,
                                'refuse_text'=>$refuse_text
                            ]);
                            $redisService->delTransactionId ($id);
                        } else { //付款
                            $messages = $message->addPayMentRefuseModel($find['transaction_code'], $find['transaction_uid']['id'], $uid, $find['id']);
                            /**
                             * 删除缓存
                             */
                            $redisService->delFlowingId ($find['id']);
                            //添加收付款日志
                            $orderTransactionLogModel->createModel([
                                'tid' => $id,
                                'content' => '付款审核驳回',
                                'submit_id' => $uid,
                                'refuse_text'=>$refuse_text
                            ]);
                            $redisService->delTransactionId ($id);
                        }

                        //发送消息
                        $gateway->sendToUid($find['transaction_uid']['id'], json_encode($messages));
                        $systemOperateLogModel->addOperate ($uid,message::$ORDER,'拒绝收付款'.$find['transaction_code']);
                        $findBoole = true;

                    }
                }
            }


            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return ReJson(message::$ERROR_CODE, $e->getMessage());
        }
        return ReJson(message::$SUCC_CODE, message::$SUCC_OPERATE);
    }


    /**
     * 获取某条订单的收付款
     * @return false|string|void
     */
    public function getDealOrderFind(
        Request                            $request,
        OrderEnterpriseCertificateBusiness $business,
        OrderOtherCertificateBusiness      $otherCerBusiness,
        OrderEnterpriseCompany             $company,
        OrderEnterpriseExchange            $exchange,
        OrderTransactionRecord             $OrderTransactionRecordModel,
        OrderPersonCertificateRegister     $certificateRegisterModel,
        OrderDeal                          $orderDealModel
    ){
        $id = $request->get('id');
        $cerCode = $request->get('certificate_code');

        if (empty($cerCode)) return ReJson(message::$ERROR_CODE, message::$ERR_PARAM_EMPTY);

        $result = [];
        //查询是否是企业证书成交
        if (!empty($business->getFinanceFieldFindModel($cerCode))) {
            $result[0] = $business->getFinanceFieldFindModel($cerCode); //应收款
            $ys = $OrderTransactionRecordModel->getOrderStatusIdYsModel($id, $cerCode);
            $yf = $OrderTransactionRecordModel->getOrderStatusIdYsModel($id, $cerCode, 1, 2);
            if ($ys) {
                $result[0]['Received'] = $ys['all_price'] ?: 0;
            } else {
                $result[0]['Received'] = 0;
            }
            $result[0]['yingfu'] = $result[0]['cost_price'];

            if ($yf) {
                $result[0]['yifu'] = $yf['all_price'] ?: 0;
            } else {
                $result[0]['yifu'] = 0;
            }

            //添加订单人才姓名
            $result[0]['person_name'] = '';
        }

        //查询是否是证书办理成交
        if (!empty($otherCerBusiness->getFinanceFieldFindModel($cerCode))) {

            $result[0] = $otherCerBusiness->getFinanceFieldFindModel($cerCode);//应收款
            $ys = $OrderTransactionRecordModel->getOrderStatusIdYsModel($id, $cerCode);
            $yf = $OrderTransactionRecordModel->getOrderStatusIdYsModel($id, $cerCode, 1, 2);
            if ($ys) {
                $result[0]['Received'] = $ys['all_price'] ?: 0;
            } else {
                $result[0]['Received'] = 0;
            }
            $result[0]['yingfu'] = $result[0]['cost_price'];

            if ($yf) {
                $result[0]['yifu'] = $yf['all_price'] ?: 0;
            } else {
                $result[0]['yifu'] = 0;
            }

        }

        //查询是否是企业证书注册
        if (!empty($company->getCertificateCodeFindModel($cerCode))) {
            $data = $company->getFinanceFieldIdFindModel($id, $cerCode);//应收款

            //获取该订单是否是续签订单
            $delData = $orderDealModel->getCerComIdFindModel($id, $cerCode);

            $result[0]['certificate_code'] = $data['certificate_code'];
            $result[0]['receivable'] = $data['price_unit'];
            $result[0]['id'] = $data['id'];
            $ys = $OrderTransactionRecordModel->getOrderStatusIdYsModel($id, $cerCode);
            if ($ys) {
                $result[0]['Received'] = $ys['all_price'] ?: 0;
            } else {
                $result[0]['Received'] = 0;
            }
            //通过人才身份证&证书专业获取人才应付价
            if (isset($data['certificate_id'][3])) {
                $cid = $data['certificate_id'][3];
            } else if (isset($data['certificate_id'][2])) {
                $cid = $data['certificate_id'][2];
            } else if ($data['certificate_id'][1]) {
                $cid = $data['certificate_id'][1];
            } else {
                $cid = $data['certificate_id'][0];
            }
            $personFind = $certificateRegisterModel->getPersonIdCardCerOrderModel($data['person_id_card'], $cid);

            if ($delData === '续签') { //续签拿收证价
                $result[0]['yingfu'] = $data['contract_price'] ?: 0;
            } else {
                $result[0]['yingfu'] = $personFind['person_price'] ?: 0;
            }

            $yf = $OrderTransactionRecordModel->getOrderStatusIdYsModel($id, $cerCode, 1, 2);
            if ($yf) {
                $result[0]['yifu'] = $yf['all_price'] ?: 0;
            } else {
                $result[0]['yifu'] = 0;
            }

            //添加订单人才姓名
            $result[0]['person_name'] = $data['person_name'];
        }

        //查询是否是倒证成交
        if (!empty($exchange->getFinanceFieldFindModel($cerCode))) {
            $data = $exchange->getFinanceFieldFindModel($cerCode);//应收款

            $result[0]['certificate_code'] = $data['certificate_code'];
            $result[0]['receivable'] = $data['out_price'] ?: 0;
            $result[0]['id'] = $data['id'];
            $ys = $OrderTransactionRecordModel->getOrderStatusIdYsModel($id, $cerCode);
            if ($ys) {
                $result[0]['Received'] = $ys['all_price'] ?: 0;
            } else {
                $result[0]['Received'] = 0;
            }

            $result[0]['yingfu'] = $data['get_price'];
            $yf = $OrderTransactionRecordModel->getOrderStatusIdYsModel($id, $cerCode, 1, 2);
            if ($yf) {
                $result[0]['yifu'] = $yf['all_price'] ?: 0;
            } else {
                $result[0]['yifu'] = 0;
            }
            //添加订单人才姓名
            $result[0]['person_name'] = $data['person_name'];
        }

        //前段需要 硬性拼接返回
        $result[0]['price'] = 0; //本次收款金额
        $result[0]['notes'] = '';//备注
        return ReJson(message::$SUCC_CODE, message::$SUCC_SELECT_DATA, $result);
    }


    //查询催款记录
    public function getPressMoneyInfo(Request $request, \app\model\UserMessage $message)
    {
        $certificate_code = $request->get('certificate_code');
        $id = $request->get('id');
        $uid = $request->get('user_login_id');
        if (empty($id) || empty($certificate_code)) return ReJson(message::$ERROR_CODE, message::$ERR_PARAM_EMPTY);

        $result = $message->getPressReasonDataModel($id, $certificate_code);
        return ReJson(message::$SUCC_CODE, message::$SUCC_SELECT_DATA, $result);
    }

    //删除业绩总表
    public function delIdFin(
        Request                            $request,
        OrderEnterpriseCompany             $company,
        OrderEnterpriseExchange            $exchange,
        OrderOtherCertificateBusiness      $other,
        OrderEnterpriseCertificateBusiness $business,
        FinanceServer                      $financeServer,
        OrderDeal                          $orderDeal,
        SystemOperateLog $systemOperateLogModel,
        RedisService $redisService
    ){

        $uid = $request->post ('user_login_id');
//        $certificate_code = $request->post ('certificate_code');
        $param = $request->post();
        //删除用户登录id
        unset($param['user_login_id']);

        $logStr = '';
        foreach ($param as $item) {
            $reKey = '';
            $data = '';
            if (empty($item['id']) || empty($item['certificate_code'])) {
                return ReJson(message::$ERROR_CODE, message::$ERR_PARAM_EMPTY);
            }
            //获取业务分类
            $type = $financeServer->getCertificateType($item['certificate_code']);

            if ($type == 1) {
                //删除业务
                $company->delIdData($item['id']);
                $logStr .= '公司证书ID：'.$item['id'].',';
                $reKey = 'order_gszs';
                $data = $company->getCertificateCodeFindModel ($item['certificate_code']);
            }
            if ($type == 2) {
                //删除业务
                $exchange->delIdModel($item['id']);
                $logStr .= '倒证ID：'.$item['id'].',';
                $reKey = 'order_dz';
                $data = $exchange->getFinanceFieldFindModel ($item['certificate_code']);
            }
            if ($type == 3) {
                //删除业务
                $other->delDataModel($item['id']);
                $logStr .= '证书办理ID：'.$item['id'].',';
                $reKey = 'order_zsbl';
                $data = $other->getCerDataModel ($item['certificate_code']);
            }
            if ($type == 4) {
                //删除业务
                $business->delIdData($item['id']);
                $logStr .= '企业证书ID：'.$item['id'].',';
                $reKey = 'order_qyzs';
                $data = $business->getCerDataModel ($item['certificate_code']);
            }
            //订单缓存
            $redisService->delOrderDzData ($data, $reKey);
            $redisService->delPerformance ($data);
        }

        $systemOperateLogModel->addOperate ($uid,message::$CWGL,'业绩总表删除'.$logStr);
        return ReJson(message::$SUCC_CODE, message::$SUCC_DATELE_DATA);
    }

    /**
     * 修改退单以后业绩与原因
     */

    public function editCharPerText(
        Request                $request,
        OrderDealChargeback    $chargebackModel,
        \app\model\UserMessage $messageModel,
        Gateway                $gateway,
        User                   $userModel,
        System                 $systemService,
        FinanceServer          $financeServerService,
        RedisService $redisService,
        SystemOperateLog $systemOperateLogModel,
        OrderEnterpriseCompany $enterpriseCompanyModel,
        OrderEnterpriseExchange $orderEnterpriseExchangeModel,
        OrderOtherCertificateBusiness $otherCertificateBusinessModel,
        OrderEnterpriseCertificateBusiness $orderEnterpriseCertificateBusinessModel
    ){
        $param = $request->post();
        $uid = $request->post('user_login_id');

        if (empty($param['id']) || empty($param['certificate_code']) || empty($param['charge_mess_uid'])) {
            return ReJson(message::$ERROR_CODE, message::$ERR_PARAM_EMPTY);
        }
        //修改退单业绩
        $chargebackModel->editDataModel($param['id'], $param['certificate_code'], [
            'performance' => $param['performance'],
            'reason' => $param['reason'],
            'charge_mess_uid' => $param['charge_mess_uid']
        ]);

        //查询修改后数据
        $cData = $chargebackModel->getOrderIdCerIsChargeBackModel ($param['id'],$param['certificate_code']);
        //退单create_date 为sale_date
        $cData['sale_date'] = $cData['create_date'];
        /**
         * 修改缓存
         */
        //获取退单日期 **报表中心缓存
        $charData = $chargebackModel->getCidCerDataModel ($param['id'], $param['certificate_code']);
        if ($charData['charge_back_date']){
            $redisService->updateDateBetweenData ($charData['charge_back_date'],$charData['charge_back_date']);
        }
        $data  = [];
        //前段如果传来b_type 证明分类为业绩总表
        if (isset($param['b_type'])) {
            $b_type = $param['b_type'] ?: $financeServerService->getCertificateType($param['certificate_code']) + 1;
        } else {
            $b_type = $financeServerService->getCertificateType($param['certificate_code']) + 1;
        }
        if ($b_type == 2){
            //订单缓存
            $redisService->delOrderDzData ($cData, "order_gszs");
            $data = $enterpriseCompanyModel->getCertificateCodeFindModel ($param['certificate_code']);
        }elseif ($b_type == 3){
            //订单缓存
            $redisService->delOrderDzData ($cData, "order_dz");
            $data = $orderEnterpriseExchangeModel->getFinanceFieldFindModel ($param['certificate_code']);
        }elseif ($b_type == 4){
            //订单缓存
            $redisService->delOrderDzData ($cData, "order_zsbl");
            $data = $otherCertificateBusinessModel->getCerDataModel ($param['certificate_code']);
        }elseif ($b_type == 5){
            //订单缓存
            $redisService->delOrderDzData ($cData, "order_qyzs");
            $data = $orderEnterpriseCertificateBusinessModel->getCerDataModel ($param['certificate_code']);
        }

        //删除业绩总表缓存
        $redisService->delPerformance ($data);

        //业务分类是否正确
        if (!in_array($b_type, [2, 3, 4, 5, 99])) {
            return ReJson(message::$ERROR_CODE, message::$ERR_BUSINESS);
        }

        $department = $systemService->getOrgStrName($userModel->getUidOrgId($uid));
        $nickname = $userModel->getIdNickName($uid);
        $mess = '"' . $param['certificate_code'] . '"' . '订单由 "' . $department . '/' . $nickname . '"' . '修改了退单业绩为 "' . $param['performance'] . '"';
        //发送消息推送
        $messages = $messageModel->editChargebackModel(
            $param['charge_mess_uid'],
            $param['certificate_code'],
            $uid,
            $b_type,
            $mess,
            $param['id']
        );

        $gateway->sendToUid($param['charge_mess_uid'], json_encode($messages));
        $systemOperateLogModel->addOperate ($uid,message::$ORDER,'退单业绩原因修改'.$param['certificate_code']);

        return ReJson(message::$SUCC_CODE, message::$SUCC_OPERATE);

    }

    /**
     * 修改收付款审批意见
     */
    public function setRefuseText(
        Request                $request,
        OrderTransactionRecord $transactionRecordModel,
        OrderTransactionLog    $orderTransactionLogModel,
        SystemOperateLog $systemOperateLogModel,
        RedisService $redisService
    ){
        $id = $request->post ('id');
        $text = $request->post ('refuse_text');
        $uid = $request->post ('user_login_id');
        $content = $request->post ('content');
        if (!$text){
            $text = "";
        }
        if (empty($id)) return ReJson (message::$ERROR_CODE,message::$ERR_PARAM_EMPTY);

        if ($orderTransactionLogModel->editIdDataModel ($id,$content,['refuse_text'=>$text])){ //修改日志
            $transactionRecordModel->editDataModel ($id,['refuse_text'=>$text]);//修改流程
            /**
             * 删除收付款缓存
             */
            $redisService->delFlowingId ($id);
            return ReJson (message::$SUCC_CODE,message::$SUCC_UPDATE);
        }
        $systemOperateLogModel->addOperate ($uid,message::$ORDER,'修改收付款审批意见'.$id.':'.$text);
        return ReJson (message::$SUCC_CODE,message::$SUCC_UPDATE);
    }
}